-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Nested class #652
Nested class #652
Conversation
Hmm, this makes the lookup behavior even too flexible. This code is now accepted, even though it generates a
Python doesn't look through the namespaces of enclosing classes (i.e., lexical scoping doesn't work for nested classes). A better way to fix this would be to use the namespace of the immediate enclosing class, but only when analyzing base classes (or function type annotations). For example:
|
I'm thinking of analysing the base classes before the setup is done for the analysis of (in you example) class C. That seems the most natural way too me and leaves the lookup function untouched. Simple switching the base class analysis and the setup (calls to |
… looked up in the enclosing classes namespace.
Now, the issue should be fixed (and similar issues with class decorators and meta classes) without making the lookup too permissive. In the process I found another case where mypy is too permissive (already before my changes): class A:
a = A() # Should raise an error, but doesn't
class B:
class C(B): pass # Should raise an error, but doesn't This is not so easy to fix because we do want to allow |
This is still work in progress, but I'd like some ideas about something. I'm making some fixes lookup problems. One fix is for the problem that mypy currently allows the use of symbols before they're defined, like this: x = A()
class A: pass I made some changes to report errors in those cases (again, still WIP). The problem is that now hundreds of test cases are failing because they contain invalid Python code like the snippet above. Does anyone have a smart idea for fixing those test cases, other than fixing them manually one by one? |
@kirbyfan64 See the Travis output: https://travis-ci.org/JukkaL/mypy/jobs/61891814 Note that some failures are 'real'. I'll try to resolve those first. |
Ah, yes, the test cases assume that you can refer to a class before defining it :-( Fixing most of the test cases could be possible via a script that works like this. Not sure if this would actually be less work than fixing the test cases manually, but at least it would be less boring:
The above steps aren't guaranteed to work, but they should (hopefully) work for the vast majority of cases. I can volunteer to write the script for step 2. |
I've removed the last bunch of commits, because they were really related to a separate issue. I thought that was quick to fix, but the test cases make it more difficult. I also want to refactor the handling environments/scopes and lookup before addressing it. So, now the PR only contains a fix for #565 and can be merged (after a review) as far as I'm concerned. Apologies for making a mess of this PR. |
No problem! Now the PR looks good. Would you like to create a new issue for using names before they are defined? |
I've made #686 for the using-names-before-their-definition issue. |
Fixes #565.