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

Strange behavior when superclass is an unknown argument #1069

Open
gvanrossum opened this issue Dec 11, 2015 · 4 comments
Open

Strange behavior when superclass is an unknown argument #1069

gvanrossum opened this issue Dec 11, 2015 · 4 comments
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-inheritance Inheritance and incompatible overrides

Comments

@gvanrossum
Copy link
Member

Forking this issue from #1067. Example:

def outer(cls):
    class C(cls):  # E: Invalid type "cls"
        def meth(self):
            super(C, self).meth()  # E: "meth" undefined in superclass

Neither error goes away when I annotate the 'cls' argument as having type 'Any'. Also interesting: this gives the same two errors:

cls = ...
class C(cls):  # E: Invalid type "cls"
    def meth(self):
        super(C, self).meth()  # E: "meth" undefined in superclass

but here annotating 'cls' as type 'Any' does make the errors disappear.

@JukkaL JukkaL added the bug mypy got something wrong label Dec 13, 2015
@elazarg
Copy link
Contributor

elazarg commented Apr 3, 2017

I can't reproduce it now.

@gvanrossum
Copy link
Member Author

I never get "meth" undefined in superclass. Instead I get two errors on the class definition, Invalid type "cls" and Invalid base class. It is still the case that inside a function annotating the argument with cls: Any doesn't make a difference but at the global level it does. So I think there is still something here, and I'd like to leave this open.

Original example:

(v35) bash-3.2$ cat -n __tmp__.py
     1	from typing import Any
     2	
     3	def f(cls):
     4	    class C(cls):
     5	        def meth(self):
     6	            super(C, self).meth()
(v35) bash-3.2$ my __tmp__.py
(v35) bash-3.2$ 

I think this is silent simply because we suppress all errors inside unannotated functions.

Adding an annotation cls: Any:

(v35) bash-3.2$ cat -n __tmp__.py
     1	from typing import Any
     2	
     3	def f(cls: Any):
     4	    class C(cls):
     5	        def meth(self):
     6	            super(C, self).meth()
(v35) bash-3.2$ my __tmp__.py
__tmp__.py:4: error: Invalid type "cls"
__tmp__.py:4: error: Invalid base class
(v35) bash-3.2$ 

This is like the original but with a slightly different set of errors.

Outside a function:

(v35) bash-3.2$ cat -n __tmp__.py
     1	from typing import Any
     2	
     3	cls = ...
     4	class C(cls):
     5	    def meth(self):
     6	        super(C, self).meth()
(v35) bash-3.2$ my __tmp__.py
__tmp__.py:4: error: Invalid type "__tmp__.cls"
__tmp__.py:4: error: Invalid base class
(v35) bash-3.2$ 

Outside a function with an added cls: Any annotation:

(v35) bash-3.2$ cat -n __tmp__.py
     1	from typing import Any
     2	
     3	cls = ...  # type: Any
     4	class C(cls):
     5	    def meth(self):
     6	        super(C, self).meth()
(v35) bash-3.2$ my __tmp__.py
(v35) bash-3.2$ 

@JukkaL JukkaL added false-positive mypy gave an error on correct code priority-1-normal labels May 18, 2018
@AlexWaygood AlexWaygood added the topic-inheritance Inheritance and incompatible overrides label Apr 3, 2022
@erictraut
Copy link

I think this issue may have been addressed at some point. I'm not able to repro it with the latest mypy.

@ilevkivskyi
Copy link
Member

The original example still fails with --check-untyped-defs, but now adding an explicit Any type fixes the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-inheritance Inheritance and incompatible overrides
Projects
None yet
Development

No branches or pull requests

6 participants