Commit Graph
3 Commits
Author SHA1 Message Date
Alex Waygood ef08e25617 [ty] Format lint documentation the same way we format mdtests (#28135) 2026-08-28 18:18:07 +01:00
Charlie Marsh d88e39067c [ty] Fix subtyping/assignability for @property protocol members (#25332)
## Summary

This PR revives #19936, which @AlexWaygood originally worked on, to add
support for `@property` protocol members.

We now represent every protocol member through four optional
capabilities:
1. The types read from a member through an instance.
2. The types written to a member through an instance.
1. The types read from a member through an implementing class.
2. The types written to a member through an implementing class.

Reads are checked covariantly, and writes are checked contravariantly.
This unifies properties with ordinary attributes while preserving the
differences between mutable instance attributes, methods, `ClassVar`,
and `Final`.

Read-only properties can be satisfied by compatible attributes or
properties. Writable properties additionally require a compatible real
write path through a property setter, custom descriptor, declared
attribute, or `__setattr__`. We share a bunch of pure attribute-write
logic with "real" assignments while keeping the consumers separate.
(It's a bit tedious, but assignments need to perform contextual
inference and emit diagnostics; protocols do not.)

A few notes following up on my live conversation with @AlexWaygood:

- A protocol `Final[T]` member is now instance-readable and
non-writable, and it can be declared without a default.
- `ClassVar[Final[T]]` additionally requires class-level readability
while remaining non-writable, and the redundant `ClassVar`/`Final`
diagnostic is suppressed for protocol bodies as it already is for
dataclasses.
- Existing terminal property-setter and `__setattr__` assignment
behavior is preserved; I did not _add_ separate semantics for arbitrary
descriptors whose `__set__` returns `Never`.

And a few notes on the boundaries of what's supported in this PR (and
what's not):

- A `ClassVar[T]` protocol member requires `T` to be readable through
both the instance and the implementing class. It cannot be written
through an instance, but a mutable `ClassVar` requires a compatible
class-level write path. `ClassVar[Final[T]]` requires both reads and
neither write.
- An instance-only attribute or property does not currently satisfy a
`ClassVar` member. (The existing conformance-related TODO for
instance-only declarations remains unresolved.)
- A callable `ClassVar` remains an attribute, not a method: both
accesses expose the same callable, whereas a method has distinct
bound-instance and unbound-class forms.
- A regular protocol method can be implemented by a compatible instance
method, `classmethod`, or `staticmethod`. This works for both subtyping
and assignability.
- Protocol members themselves declared using `@classmethod` or
`@staticmethod` remain unsupported and are represented internally using
`Todo`.
- General `type[P]` and meta-protocol behavior remains incomplete. The
class-side capabilities introduced here primarily model structural
compatibility; they do not imply complete support for ordinary access
through `type[P]`.

Closes https://github.com/astral-sh/ty/issues/1379.
Closes https://github.com/astral-sh/ty/issues/1380.
2026-07-06 10:49:54 -04:00
Micha Reiser 950d163bf0 [ty] Extract lint rule documentation into markdown files and test example code (#25863) 2026-06-16 13:53:32 +00:00