Skip to content

Commit

Permalink
Mypy improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinche committed Mar 14, 2022
1 parent 2a703cc commit b1ff388
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
11 changes: 6 additions & 5 deletions src/attr/__init__.pyi
Expand Up @@ -8,6 +8,7 @@ from typing import (
List,
Mapping,
Optional,
Protocol,
Sequence,
Tuple,
Type,
Expand Down Expand Up @@ -57,6 +58,10 @@ _CompareWithType = Callable[[Any, Any], bool]
# _ValidatorType from working when passed in a list or tuple.
_ValidatorArgType = Union[_ValidatorType[_T], Sequence[_ValidatorType[_T]]]

# A protocol to be able to statically accept an attrs class.
class AttrsClass(Protocol[_T]):
__attrs_attrs__: _T

# _make --

NOTHING: object
Expand Down Expand Up @@ -399,11 +404,7 @@ def define(
mutable = define
frozen = define # they differ only in their defaults

# TODO: add support for returning NamedTuple from the mypy plugin
class _Fields(Tuple[Attribute[Any], ...]):
def __getattr__(self, name: str) -> Attribute[Any]: ...

def fields(cls: type) -> _Fields: ...
def fields(cls: type[AttrsClass[_T]]) -> _T: ...
def fields_dict(cls: type) -> Dict[str, Attribute[Any]]: ...
def validate(inst: Any) -> None: ...
def resolve_types(
Expand Down
13 changes: 12 additions & 1 deletion tests/test_mypy.yml
Expand Up @@ -316,7 +316,6 @@
class Confused:
...
- case: testAttrsInheritance
main: |
import attr
Expand Down Expand Up @@ -1393,3 +1392,15 @@
foo = x
reveal_type(B) # N: Revealed type is "def (foo: builtins.int) -> main.B"
- case: testFields
main: |
from attr import define, fields
@define
class A:
a: int
b: str
reveal_type(fields(A)) # N: Revealed type is "Tuple[attr.Attribute[builtins.int], attr.Attribute[builtins.str]]"
fields(A).a

0 comments on commit b1ff388

Please sign in to comment.