Jaymon / prom

A PostgreSQL or SQLite orm for Python
MIT License
22 stars 4 forks source link

Relative classpaths #151

Open Jaymon opened 1 year ago

Jaymon commented 1 year ago

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:

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 commented 8 months ago

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")
#