-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Allow Dynamic Attribute Access for Namespaces for Improved Typing #14475
Comments
Happy to put up a PR if this is something we're interested in. |
I have yet to test this, but it would look something like this. Draft PR: #14476 |
This is something that is brought up often on Stack Overflow and on our Discord. If there's an elegant workaround, that would be great. I am not sure how I feel about adding a namespace just for working around this issue. If you have something that works, I'd be happy to review / play around with it so I can make up my mind. |
#13899 related, which has info on some existing workarounds. @max-muoto by opting out of typing, is this also not providing code completion? Personally, I think the ergonomic benefits of accessors are outweighed by the lack of information the methods will have when compared to the rest of I'd really appreciate a simpler solution than polugins, if possible |
Just checking I'm reading this sentence correctly: are you essentially saying "the lack of typing / code completion negates the ergonomic benefits of accessors, so accessors are not worth it"? |
Yeah that's what I was going for @MarcoGorelli, sorry it wasn't clear. The only thing I'd add is I'm talking about 3rd-party accessors, in their current form. It's the contrast between the two, currently, that I was trying to highlight. Which I'm unsure if this proposal would resolve (but would really appreciate if it did) |
I'm not well versed on typing and linting so bear with me. Would this make type hinting work or it'll just keep linters from complaining about non-existent attributes or something else? |
Sounds good, I'll wrap up my PR and ping you here once that's good to look at. Thanks! |
This would mainly serve as a way of stopping your type-checker from complaining about these attributes, but it wouldn't give specific return annotations, instead returning |
Yes, unlike A MyPy plugin would likely work well for MyPy users, but having something compatible with the native Python typing system is important for Pyright users. |
As pointed out by @alexander-beedie in the linked PR, this doesn't look like the right solution to the problem. Users can either ignore the type errors explicitly, or use something like I'll close this issue as not planned, specifically because the proposed solution is not desirable. |
I have a reasonable solution to this I believe. I have published a package for it. Very alpha but it does the main thing. |
@stinodego can you review the concept of my proposed solution to this issue? at your convenience of course :) |
Description
Currently if you follow the example for defining a custom DataFrame namespace:
split
will be flagged as being an invalid attribute access when using Pyright or MyPy, since this is dynamically added onto the DataFrame.We could add general dynamic attribute access to get around this:
I do see this being problematic from the perspective of having to over over to see if the attribute actually exists in case you were to mistype an actual attribute on
DataFrame
.Perhaps, we could add an explicit namespace lookup table to avoid this.
This way, one can opt out of strict typing for these dynamic namespaces without having to add typing ignore comments. We could still keep in the old way of accessing these namespaces for those who don't care about the typing. This has been flagged on StackOverflow as an issue for reference: https://stackoverflow.com/questions/77733446/polars-api-registering-and-type-checkers
The text was updated successfully, but these errors were encountered: