## Summary
Users can now suppress an attribute error that affects only some union
members and still catch names that are missing from every member. For
example, both lines below previously reported `unresolved-attribute`:
```python
def clean(value: str | int) -> str:
value.stirp() # missing-attribute: neither str nor int has stirp
return value.strip() # missing-union-attribute: str has strip, int does not
```
The motivation is that a project adopting ty may want `value.strip()` on
`str | None` to only warn while a typo remains an error-severity
diagnostic.
Both rules default to errors. The same distinction applies to optional
values such as `str | None` and to attribute deletion. Attributes
defined only conditionally on a class continue to use
`possibly-missing-attribute`.
### Comparison to other type checkers
Pyright distinguishes between `reportOptionalMemberAccess` (which is
specifically for unions with `None`) and `reportAttributeAccessIssue`,
which is for all other unions. mypy, meanwhile, distinguishes between
`union-attr` (which is an error code for all attribute-access errors on
union types) and `attr-defined` (which is an error code for all
attribute-access errors on non-union types). So this new split doesn't
_exactly_ align with either precedent -- but I _do_ think it's probably
the most useful split we can do, and it's the one that makes the most
sense _a priori_ in my opinion.
### Compatibility
`unresolved-attribute` remains an alias for `missing-attribute` in
configuration and inline ignores. To preserve a setting such as
`unresolved-attribute = "warn"` for both cases, also set
`missing-union-attribute = "warn"`. On an access that fails for only
some union members, replace `# ty: ignore[unresolved-attribute]` with `#
ty: ignore[missing-union-attribute]`. Tools that consume diagnostic
codes should recognize both new names.
Fixes astral-sh/ty#2839 and fixes astral-sh/ty#2471.
## Test Plan
A comparison with ty 0.0.96 on typeshed-stats, docstring-adder,
sphinx-lint, and typing_extensions found the same messages, locations,
and severities. Eight diagnostics received `missing-union-attribute`
across those four projects (two in sphinx-lint and six in
typing_extensions).
---------
Co-authored-by: Brent Westbrook <brentrwestbrook@gmail.com>
2.8 KiB
Generic aliases
Type expressions
We recognize if a types.GenericAlias instance is created by specializing a generic class. We don't
explicitly mention it in our type display, but list[int] in the example below is a GenericAlias
instance at runtime:
Numbers = list[int]
# At runtime, `Numbers` is an instance of `types.GenericAlias`. Showing
# this as `list[int]` is more helpful, though:
reveal_type(Numbers) # revealed: <class 'list[int]'>
import types
from typing_extensions import TypeForm
generic_alias: types.GenericAlias = list[int]
generic_alias_typeform: TypeForm = list[int]
def _(numbers: Numbers) -> None:
reveal_type(numbers) # revealed: list[int]
It is also valid to create GenericAlias instances manually:
from types import GenericAlias
Strings = GenericAlias(list, (str,))
reveal_type(Strings) # revealed: GenericAlias
However, using such a GenericAlias instance in a type expression is currently not supported:
# error: [invalid-type-form] "Variable `Strings` cannot be used as a type"
def _(strings: Strings) -> None:
reveal_type(strings) # revealed: Unknown
Attributes of type aliases
The alias objects type[Any] and typing.Type[Any] delegate attribute access to their origin,
type. They do not have arbitrary attributes, even though their type argument is dynamic.
from typing import Any, Type, TypeAlias
Modern: TypeAlias = type[Any]
Legacy: TypeAlias = Type[Any]
Modern.missing # error: [missing-attribute]
Legacy.missing # error: [missing-attribute]
reveal_type(Modern.__name__) # revealed: str
reveal_type(Legacy.__name__) # revealed: str
Attributes belonging to the alias itself remain accessible.
reveal_type(Modern.__args__) # revealed: tuple[Any, ...]
reveal_type(Legacy.__args__) # revealed: tuple[Any, ...]
Modern.__origin__
Legacy.__origin__
Modern.__mro_entries__((object,))
The origin is type regardless of the type argument. Attributes of int are not available on the
alias object type[int].
Integers = type[int]
LegacyIntegers = Type[int]
Integers.bit_length # error: [missing-attribute]
LegacyIntegers.bit_length # error: [missing-attribute]
When these aliases are used as annotations, their inhabitants can be arbitrary class objects. Accessing an unknown attribute through such a parameter remains valid.
def dynamic_class(modern: Modern, legacy: Legacy) -> None:
reveal_type(modern.missing) # revealed: Any
reveal_type(legacy.missing) # revealed: Any
Attributes of arbitrary GenericAlias instances
When the origin is unknown, we allow arbitrary attribute access through a GenericAlias instance.
from types import GenericAlias
def unknown_origin(alias: GenericAlias) -> None:
reveal_type(alias.missing) # revealed: Any