Skip to content

Mutable field invariance #5933

Closed
Closed

Description

class UStr(str):
    pass


class A:
    foo: str


class B(A):
    foo: UStr

This should and does not produce an error. Specifically, B is an invalid subclass of A. See: python/typing#1458 (comment) and python/mypy#3208 for context.

The unsafe nature shows up if B has a reliance on the subclass, but a function taking A modifies foo as only a string. This should be handled by users with a generic instead, and this should be invariant as lists are for the same reasons.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions