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

Non-Protocol Intersection #12

Closed
gentlegiantJGC opened this issue May 9, 2024 · 3 comments
Closed

Non-Protocol Intersection #12

gentlegiantJGC opened this issue May 9, 2024 · 3 comments

Comments

@gentlegiantJGC
Copy link

Is there a reason why this can't be used with non-protocol classes?
This seems like the solution I have been looking for but I need it to work with non-protocol classes.

I hope that one day Python will have a native intersection type but until then this could be a useful stopgap.

@klausweiss
Copy link
Owner

Is there a reason why this can't be used with non-protocol classes?

The implementation is explicitly only targeting protocols, as it's a relatively simple problem to solve. This plugin will only ever support intersection of protocols (hence the name ;)).

General type intersection is a much more involving problem. There's a discussion on the topic in the python/typing repository python/typing#213 and there's actually a PEP in the works to make this happen https://github.com/CarliJoy/intersection_examples I remember reading excellent examples as to why it's not so simple somewhere in that thread, but I don't have them at hand anymore unfortunately, you'll have to dig if you're interested.

@gentlegiantJGC
Copy link
Author

Do you know if a MyPy plugin exists/could be written to allow a normal class to be promoted to a protocol so that it could be used with your intersection?

I have been experimenting with your code and setting is_protocol=True on all the classes passed to the intersection allows your plugin to work with non-protocol classes however this effects the class outside of the intersection.

@klausweiss
Copy link
Owner

Do you know if a MyPy plugin exists/could be written to allow a normal class to be promoted to a protocol so that it could be used with your intersection?

Sorry, I don't know.

I have been experimenting with your code and setting is_protocol=True on all the classes passed to the intersection allows your plugin to work with non-protocol classes however this effects the class outside of the intersection.

I would advise against modifying this plugin in such way (making it do things it's not supposed to do), unless you're familiar with internals of mypy itself. The goal of a type checker ultimately is to make you trust the code more. If you can't trust the type-checker, it misses the point.

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

2 participants