Thanks to visit codestin.com
Credit goes to github.com

Skip to content

TypedDict attributes are always objects with TypeVar #15071

Description

@vorpal56

Bug Report

All attributes of TypedDict are "object", not the annotated type when using TypeVar

To Reproduce

class A(TypedDict):
    x: int
    y: str

class B(A):
    z: Dict[str, str]

T = TypeVar('T', bound=A)

def my_func(things: List[T]):
    for t in things:
        x_val: int = t['x'] # Incompatible types in assignment (expression has type "object", variable has type "int")
        y_val: str = t['y'] # Incompatible types in assignment (expression has type "object", variable has type "str")

my_things: List[B] = [
    {'x': 1, 'y': '2', 'z': {'foo': 'bar'}},
    {'x': 1, 'y': '2', 'z': {'foo': 'baz'}},
]
my_func(my_things)

Can do the same but with my_things: List[A] passed into the same function.
Doing something like z_val: Dict[str, str] = my_b_type['z'].get('foo') raises an error: "object" has no attribute "get"

Expected Behavior

It respects the type annotations and has access to attributes/methods of the type.

Actual Behavior

error: Incompatible types in assignment (expression has type "object", variable has type "int")
error: Incompatible types in assignment (expression has type "object", variable has type "str")
error: "object" has no attribute "get"

Your Environment

  • Mypy version used: 0.812
  • Mypy configuration options from mypy.ini (and other config files): n/a
  • Python version used: 3.8.13

Activity

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

    bugmypy got something wrong

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions