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

Generic Type[T] function argument rejected as base class #14458

Open
tyilo opened this issue Jan 16, 2023 · 3 comments
Open

Generic Type[T] function argument rejected as base class #14458

tyilo opened this issue Jan 16, 2023 · 3 comments
Labels
bug mypy got something wrong

Comments

@tyilo
Copy link

tyilo commented Jan 16, 2023

Bug Report

Since #5865 was fixed, the following code now type checks:

from typing import Any, Type


def f(typ: Type[Any]) -> Type[Any]:
    class C(typ):
        pass

    return C

However if we make the superclass generic, it doesn't type check anymore which I would expect it to do.

To Reproduce

from typing import Type, TypeVar

T = TypeVar("T")


def f(typ: Type[T]) -> Type[T]:
    class C(typ):
        pass

    return C

Actual Behavior

mypy_return_type.py:7: error: Variable "typ" is not valid as a type  [valid-type]
mypy_return_type.py:7: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
mypy_return_type.py:7: error: Invalid base class "typ"  [misc]
Found 2 errors in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.0.0+dev.e9f5858c44b61c2d02819940debe3c31d099f9d5
  • Mypy command-line flags: mypy mypy_return_type.py
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.10.9
@tyilo tyilo added the bug mypy got something wrong label Jan 16, 2023
@nathanjmcdougall
Copy link

nathanjmcdougall commented Apr 10, 2023

I think the following similar example using Protocol should also typecheck, but fails in a similar way:

from typing import Protocol, Type

class P(Protocol):
    pass

def f(typ: Type[P]) -> Type[P]:
    class C(typ):
        pass

    return C

I'm very new to this though, so I'm not sure.

@baileywickham
Copy link

From this comment on #5865, this still fails

from typing import TypeVar

C = TypeVar("C", bound="MyClass")


class MyClass:
    class InnerClass:
        pass

    @classmethod
    def dynamic_subclass(cls: type[C]) -> type[C]:
        # Mypy emits two errors on the following line:
        # error: Variable "cls" is not valid as a type  [valid-type]
        # error: Invalid base class "cls"  [misc]
        class MySubClass(cls):
            # Mypy emits one error on the following line:
            # Name "cls.InnerClass" is not defined  [name-defined]
            class MySubInnerClass(cls.InnerClass):
                pass

            pass
        return MySubClass


# to show that everything works as expected at runtime:
SubClass = MyClass.dynamic_subclass()
my_subclass_instance = SubClass()

with errors

../../test.py:15: error: Variable "cls" is not valid as a type  [valid-type]
../../test.py:15: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
../../test.py:15: error: Invalid base class "cls"  [misc]
../../test.py:18: error: Name "cls.InnerClass" is not defined  [name-defined]
Found 3 errors in 1 file (checked 1 source file)

From what I can tell, these errors are related to this issue.

erictraut pushed a commit to microsoft/pyright that referenced this issue May 15, 2023
…ic base class in a `class` statement where the dynamic base class is based on a bound type variable. This addresses a bug that was reported in the mypy issue tracker: python/mypy#14458.
@TLCFEM
Copy link

TLCFEM commented Sep 19, 2024

Also, example.

from typing import cast, TypeVar

T = TypeVar('T')


def cast_as(cls: type[T], a) -> T:
    return cast(cls, a)
OYSL.py:7: error: Variable "cls" is not valid as a type  [valid-type]
OYSL.py:7: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
Found 1 error in 1 file (checked 1 source file)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

4 participants