Skip to content

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

Closed
@sterliakov

Description

@sterliakov

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions