File tree Expand file tree Collapse file tree 1 file changed +64
-0
lines changed
crates/ty_python_semantic/resources/mdtest Expand file tree Collapse file tree 1 file changed +64
-0
lines changed Original file line number Diff line number Diff line change @@ -1628,6 +1628,70 @@ date.year = 2025
16281628date.tz = " UTC"
16291629```
16301630
1631+ ### Setting attributes on unions
1632+
1633+ Setting attributes on unions where all elements of the union have the attribute is acceptable
1634+
1635+ ``` py
1636+ from typing import Union
1637+
1638+ class A :
1639+ x: int
1640+
1641+ class B :
1642+ x: int
1643+
1644+ C = Union[A, B]
1645+
1646+ a: C = A()
1647+ a.x = 42
1648+ ```
1649+
1650+ Setting attributes on unions where any element of the union does not have the attribute reports
1651+ possibly unbound
1652+
1653+ ``` py
1654+ from typing import Union
1655+
1656+ class A :
1657+ pass
1658+
1659+ class B :
1660+ x: int
1661+
1662+ C = Union[A, B]
1663+
1664+ a: C = A()
1665+
1666+ # instead of unresolved-attribute, this should report possibly-unbound-attribute
1667+ # TODO : error: [possibly-unbound-attribute]
1668+ # error: [unresolved-attribute]
1669+ a.x = 42
1670+
1671+ a: C = B()
1672+
1673+ # TODO : error: [possibly-unbound-attribute]
1674+ a.x = 42
1675+ ```
1676+
1677+ Setting attributes on a generic where the upper bound is a union, and not all elements of the union
1678+ have the attribute, also reports possibly unbound:
1679+
1680+ ``` py
1681+ from typing import Union, TypeVar
1682+
1683+ class A :
1684+ pass
1685+
1686+ class B :
1687+ x: int
1688+
1689+ C = TypeVar(" C" , bound = Union[A, B])
1690+
1691+ def _ (a : C):
1692+ a.x = 42 # error: [possibly-unbound-attribute]
1693+ ```
1694+
16311695### ` argparse.Namespace `
16321696
16331697A standard library example of a class with a custom ` __setattr__ ` method is ` argparse.Namespace ` :
You can’t perform that action at this time.
0 commit comments