[ty] Improve and extend tests for instance attributes redeclared in subclasses (#20866)
Some checks are pending
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / ty completion evaluation (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
CI / benchmarks instrumented (ruff) (push) Blocked by required conditions
CI / benchmarks walltime (small|large) (push) Blocked by required conditions
CI / benchmarks instrumented (ty) (push) Blocked by required conditions
CI / benchmarks walltime (medium|multithreaded) (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run

Part of https://github.com/astral-sh/ty/issues/1345
This commit is contained in:
Alex Waygood 2025-10-14 19:31:34 +01:00 committed by GitHub
parent f8e00e3cd9
commit 43eddc566f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -828,6 +828,10 @@ class Base:
redeclared_with_narrower_type: str | None
redeclared_with_wider_type: str | None
redeclared_in_method_with_same_type: str | None
redeclared_in_method_with_narrower_type: str | None
redeclared_in_method_with_wider_type: str | None
overwritten_in_subclass_body: str
overwritten_in_subclass_method: str
@ -873,6 +877,14 @@ class Intermediate(Base):
undeclared = "intermediate"
def set_attributes(self) -> None:
self.redeclared_in_method_with_same_type: str | None = None
# TODO: This should be an error (violates Liskov)
self.redeclared_in_method_with_narrower_type: str = "foo"
# TODO: This should be an error (violates Liskov)
self.redeclared_in_method_with_wider_type: object = object()
# TODO: This should be an `invalid-assignment` error
self.overwritten_in_subclass_method = None
@ -889,11 +901,13 @@ reveal_type(Derived().attribute) # revealed: int | None
reveal_type(Derived.redeclared_with_same_type) # revealed: str | None
reveal_type(Derived().redeclared_with_same_type) # revealed: str | None
# TODO: It would probably be more consistent if these were `str | None`
# We infer the narrower type here, despite the Liskov violation,
# for compatibility with other type checkers (and to reflect the clear user intent)
reveal_type(Derived.redeclared_with_narrower_type) # revealed: str
reveal_type(Derived().redeclared_with_narrower_type) # revealed: str
# TODO: It would probably be more consistent if these were `str | None`
# We infer the wider type here, despite the Liskov violation,
# for compatibility with other type checkers (and to reflect the clear user intent)
reveal_type(Derived.redeclared_with_wider_type) # revealed: str | int | None
reveal_type(Derived().redeclared_with_wider_type) # revealed: str | int | None
@ -901,6 +915,19 @@ reveal_type(Derived().redeclared_with_wider_type) # revealed: str | int | None
reveal_type(Derived.overwritten_in_subclass_body) # revealed: Unknown | None
reveal_type(Derived().overwritten_in_subclass_body) # revealed: Unknown | None | str
reveal_type(Derived.redeclared_in_method_with_same_type) # revealed: str | None
reveal_type(Derived().redeclared_in_method_with_same_type) # revealed: str | None
# TODO: both of these should be `str`, despite the Liskov violation,
# for compatibility with other type checkers (and to reflect the clear user intent)
reveal_type(Derived.redeclared_in_method_with_narrower_type) # revealed: str | None
reveal_type(Derived().redeclared_in_method_with_narrower_type) # revealed: str | None
# TODO: both of these should be `object`, despite the Liskov violation,
# for compatibility with other type checkers (and to reflect the clear user intent)
reveal_type(Derived.redeclared_in_method_with_wider_type) # revealed: str | None
reveal_type(Derived().redeclared_in_method_with_wider_type) # revealed: object
reveal_type(Derived.overwritten_in_subclass_method) # revealed: str
reveal_type(Derived().overwritten_in_subclass_method) # revealed: str