Skip to content

Default values for generic attributes are unsafe #5703

Open
@ilevkivskyi

Description

@ilevkivskyi

Currently mypy accepts this unsafe code:

from typing import Generic, TypeVar, List
  
T = TypeVar('T')

class B(Generic[T]):
    x: List[T] = []

class C1(B[int]):
    pass

class C2(B[str]):
    pass

C1().x.append(42)
C2().x.append('Hm...')

A potential solution would be to disallow all default values for generic attributes except None. This is however a low priority, since it is a false negative, and a rare corner case.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions