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

type[TypeVar] not checked properly with upper bound #14755

Closed
sterliakov opened this issue Feb 22, 2023 · 0 comments · Fixed by #14756
Closed

type[TypeVar] not checked properly with upper bound #14755

sterliakov opened this issue Feb 22, 2023 · 0 comments · Fixed by #14756
Labels
bug mypy got something wrong topic-type-variables

Comments

@sterliakov
Copy link
Contributor

sterliakov commented Feb 22, 2023

Bug Report

When checking a function with argument of type[SomeTypeVar], class attributes are considered missing.

To Reproduce

from typing import ClassVar, Type, TypeVar, cast

class One:
    PATTERN: ClassVar[str] = r"^([a-z]{3})$"

class Two:
    PATTERN: ClassVar[str] = r"^([0-9]{3})$"

_T = TypeVar("_T", bound=One | Two)

def foo(result_type: Type[_T]) -> _T:
    result_type.PATTERN  # E: "Type[_T]" has no attribute "PATTERN"  [attr-defined]
    return cast(_T, result_type())

foo(One)
foo(Two)

This doesn't depend on ClassVar and works with Final or plain str as well.

Gist: https://mypy-play.net/?mypy=master&python=3.10&gist=2d769a9767898e71431e59df1c2f8488

Expected Behavior

No errors (let's ignore cast for now, it's another unrelated issue).

Actual Behavior

main.py:12: error: "Type[_T]" has no attribute "PATTERN"  [attr-defined]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.0.0 and master
  • Mypy command-line flags: not related
  • Mypy configuration options from mypy.ini (and other config files): not related
  • Python version used: 3.9-3.11, not related

Discovered in this SO question

@sterliakov sterliakov added the bug mypy got something wrong label Feb 22, 2023
hauntsaninja pushed a commit that referenced this issue Mar 15, 2023
Fixes #14755.

This case is already covered by
`check-classes.test::testTypeUsingTypeCClassMethodFromTypeVarUnionBound`,
now we just remove from it errors that shouldn't be reported.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-type-variables
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants