Skip to content

Strange behavior when superclass is an unknown argument #1069

Open

Description

@gvanrossum

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.

Activity

  1. elazarg commented on Apr 3, 2017

    @elazarg
    Contributor

    I can't reproduce it now.

  2. gvanrossum commented on Apr 3, 2017

    @gvanrossum
    MemberAuthor

    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$ 
    
  3. erictraut commented on Aug 10, 2023

    @erictraut

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

  4. ilevkivskyi commented on Aug 12, 2023

    @ilevkivskyi
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions