prom icon indicating copy to clipboard operation
prom copied to clipboard

Relative classpaths

Open Jaymon opened this issue 2 years ago • 1 comments

First, what is a relative classpath? It's doing something like this:

class Foo(Orm):
    bar = Field("..some.classpath.to.Bar")

The beginning .. in ..some.classpath.to.Bar would be a relative classpath because it is relative from the local module.

I remember I tried to implement this and failed, this was the best commit message I could find:

https://github.com/Jaymon/prom/commit/3bc5d90ebd6897a2bb8cad8e29dfa36dfe688edb (this has all the code I had implemented for Query.ref)

sigh, reverts Query.ref being able to take a relative classpath because Field(classpath) can't be relative and there really isn't any way to ever make that work, and I would rather they be consistent

So I think the problem was the fields didn't know from what module and class they were being defined in and so couldn't do a relative import.

I think this would be worth looking into again.

Here are some other commits that might be helpful:

  • https://github.com/Jaymon/prom/commit/7b1c6f3ad200007938390f7a2e9b3c3acea9da44 - "updates utils.get_objects() to support relative classpaths also"
  • https://github.com/Jaymon/prom/commit/065f2742bc2e4c708e64b98a59e0b82bca52cb80 - "turns out I hadn't really made the relative importing useful for the query, so Query.ref will now use the Query.orm_class and its parents to attempt to resolve relative classpaths"

The thing that made me think this might be worth revisiting is pkgutil.resolve_name(name) and I also know there are some python later versions of 3+ that make it easier for the descriptor to find out the name of the field it is being created for and things like that. So relative classpaths might be possible now.

Jaymon avatar Jan 11 '23 20:01 Jaymon

I removed this code from the prom.utils.get_objects method on 1-24-2024:

#     if classpath.startswith("."):
#         rel_count = len(re.match("^\.+", classpath).group(0))
#         if calling_classpath:
#             calling_count = calling_classpath.count(".")
#             if rel_count > calling_count:
#                 raise ValueError(
#                     "Attempting relative import passed calling_classpath {}".format(
#                         calling_classpath
#                     )
#                 )
# 
#             bits = calling_classpath.rsplit('.', rel_count)
#             parent_classpath = bits[0]
#             classpath = ".".join([parent_classpath, classpath[rel_count:]])
# 
#         else:
#             raise ValueError("Attempting relative import without calling_classpath")
# 

Jaymon avatar Jan 24 '24 21:01 Jaymon