Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relative classpaths #151

Open
Jaymon opened this issue Jan 11, 2023 · 1 comment
Open

Relative classpaths #151

Jaymon opened this issue Jan 11, 2023 · 1 comment

Comments

@Jaymon
Copy link
Owner

Jaymon commented Jan 11, 2023

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:

3bc5d90 (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:

  • 7b1c6f3 - "updates utils.get_objects() to support relative classpaths also"
  • 065f274 - "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
Copy link
Owner Author

Jaymon commented Jan 24, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant