-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Catch TypeVisitor subclasses with unimplemented abstract methods #730
Comments
I am suspicious of any visitors that can meaningfully work without full implementations. Perhaps there is a need for more specific visitors? |
It appears as though there is a common pattern of unimplemented abstract methods (visit_star_type, visit_overloaded, visit_error_type, visit_erased_type), I figured that maybe they had been added after the initial implementation of all visitors, and don't appear in the test cases and as such haven't been caught. In total there are about 25 subclasses which do not implement all abstract methods, mostly it is either visit_star_type or visit_overloaded which are unimplemented. |
The current implementation is just sloppy, and is inherited from early pre-mypy days when there were no abstract methods. Using |
Use the @AbstractMethod decorator on abstract methods. Part of python#730
I've opened a PR to get the ball rolling. As mentioned in the PR, Subclasses with incomplete implementations are:
|
Use the @AbstractMethod decorator on abstract methods which are implemented in all subtypes. Part of python#730
This helps to point out the offending unimplemented abstract methods in subtypes. Part of python#730.
Use the @AbstractMethod decorator on abstract methods which are implemented in all subtypes. Part of python#730
This helps to point out the offending unimplemented abstract methods in subtypes. Part of python#730.
Actually I rethought this, while it makes sense to move towards using the I have restructured my original PR #733 with The It would also be cool to add tests which fail when all currently subtypes implement each of the abstract methods, these would indicate that the |
Use the @AbstractMethod decorator on abstract methods which are implemented in all subtypes. Part of #730
This helps to point out the offending unimplemented abstract methods in subtypes. Part of #730.
…c" types This is a nearly-complete fix to python#730 -- the trick is that some types aren't real, they're synthetic AST constructs, and so we shouldn't have to deal with those in every type visitor. Only the type visitors that deal with types before the synthetic constructs are analyzed away.
…c" types (#3204) * Use inheritance to allow most TypeVisitors not to deal with "synthetic" types This is a nearly-complete fix to #730 -- the trick is that some types aren't real, they're synthetic AST constructs, and so we shouldn't have to deal with those in every type visitor. Only the type visitors that deal with types before the synthetic constructs are analyzed away.
Is there any update on this? mypy 0.580 is stil showing this error for code: |
Are you sure you're on the right issue? This one is talking about an internal problem in mypy as far as I can tell. |
This pull request resolves python#730 -- it makes all TypeVisitor and SyntheticTypeVisitor methods abstract. This thankfully required fairly minimal changes. I ended up needing to make only three non-trivial changes: 1. The visitor for PlaceholderType was moved out of TypeVisitor into SyntheticTypeVisitor. My understanding is that PlaceholderType is a semantic analysis only type, so this ought to be safe. 2. As a consequence of 1, I ended up removing the existing PlaceholderType visitor method from TypeTranslator. (It seems that method was added about 6 months ago when PlaceholderType was first introduced, and was untouched since then). 3. TypeIndirectionVisitor was turned from a SyntheticTypeVisitor into a TypeVisitor. I believe we call this visitor only in the final pass, after the type-checking phase.
) This pull request almost resolves #730 -- it makes all TypeVisitor and SyntheticTypeVisitor methods with the exception of TypeAliasType abstract. This thankfully required fairly minimal changes. I ended up needing to make only three non-trivial changes: 1. The visitor for PlaceholderType was moved out of TypeVisitor into SyntheticTypeVisitor. My understanding is that PlaceholderType is a semantic analysis only type, so this ought to be safe. 2. As a consequence of 1, I ended up removing the existing PlaceholderType visitor method from TypeTranslator. (It seems that method was added about 6 months ago when PlaceholderType was first introduced, and was untouched since then). 3. TypeIndirectionVisitor was turned from a SyntheticTypeVisitor into a TypeVisitor. I believe we call this visitor only in the final pass, after the type-checking phase.
…thon#7312) This pull request almost resolves python#730 -- it makes all TypeVisitor and SyntheticTypeVisitor methods with the exception of TypeAliasType abstract. This thankfully required fairly minimal changes. I ended up needing to make only three non-trivial changes: 1. The visitor for PlaceholderType was moved out of TypeVisitor into SyntheticTypeVisitor. My understanding is that PlaceholderType is a semantic analysis only type, so this ought to be safe. 2. As a consequence of 1, I ended up removing the existing PlaceholderType visitor method from TypeTranslator. (It seems that method was added about 6 months ago when PlaceholderType was first introduced, and was untouched since then). 3. TypeIndirectionVisitor was turned from a SyntheticTypeVisitor into a TypeVisitor. I believe we call this visitor only in the final pass, after the type-checking phase.
Issue #638 was a pain to find because the root cause is an unimplemented abstract method which silently returns
None
and somewhere down the line causes type checking to fail. It would be better to check this explicitly. The following shows that this is done for one of the abstract methods:https://github.com/JukkaL/mypy/blob/bbb2dd4f07f995a4b28b8044fa91aa97960fe192/mypy/types.py#L532. There are two potential solutions: 1) use abc's @AbstractMethod decorator, 2) raise an error directly in TypeVisitor.
@JukkaL Do you have a preferred solution?
The text was updated successfully, but these errors were encountered: