Skip to content

Commit 0df5b9a

Browse files
committed
Improve declaration of private vs public objects in stubs
1 parent 642ddb5 commit 0df5b9a

File tree

4 files changed

+31
-31
lines changed

4 files changed

+31
-31
lines changed

src/attr/__init__.pyi

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
from typing import Any, Callable, Dict, Generic, Iterable, List, Optional, Mapping, Tuple, Type, TypeVar, Union, overload
2-
from . import exceptions
3-
from . import filters
4-
from . import converters
5-
from . import validators
2+
# `import X as X` is required to expose these to mypy. otherwise they are invisible
3+
from . import exceptions as exceptions
4+
from . import filters as filters
5+
from . import converters as converters
6+
from . import validators as validators
67

78
# typing --
89

910
_T = TypeVar('_T')
10-
T = TypeVar('T')
1111
_C = TypeVar('_C', bound=type)
1212
_M = TypeVar('_M', bound=Mapping)
1313
_I = TypeVar('_I', bound=Iterable)
1414

15-
ValidatorType = Callable[[object, 'Attribute', Any], Any]
15+
_ValidatorType = Callable[[Any, 'Attribute', Any], Any]
1616
# FIXME: Bind to attribute type?
17-
ConverterType = Callable[[Any], Any]
18-
FilterType = Callable[['Attribute', Any], bool]
17+
_ConverterType = Callable[[Any], Any]
18+
_FilterType = Callable[['Attribute', Any], bool]
1919

2020
# _make --
2121

@@ -24,37 +24,37 @@ class _CountingAttr: ...
2424
NOTHING : object
2525

2626
class Factory(Generic[_T]):
27-
factory : Union[Callable[[], _T], Callable[[object], _T]]
27+
factory : Union[Callable[[], _T], Callable[[Any], _T]]
2828
takes_self : bool
29-
def __init__(self, factory: Union[Callable[[], _T], Callable[[object], _T]], takes_self: bool = ...) -> None: ...
29+
def __init__(self, factory: Union[Callable[[], _T], Callable[[Any], _T]], takes_self: bool = ...) -> None: ...
3030

3131
class Attribute:
3232
__slots__ = ("name", "default", "validator", "repr", "cmp", "hash", "init", "convert", "metadata", "type")
33-
def __init__(self, name: str, default: Any, validator: Optional[Union[ValidatorType, List[ValidatorType]]], repr: bool, cmp: bool, hash: Optional[bool], init: bool, convert: Optional[ConverterType] = ..., metadata: Mapping = ..., type: Union[type, Factory] = ...) -> None: ...
33+
def __init__(self, name: str, default: Any, validator: Optional[Union[_ValidatorType, List[_ValidatorType], Tuple[_ValidatorType, ...]]], repr: bool, cmp: bool, hash: Optional[bool], init: bool, convert: Optional[_ConverterType] = ..., metadata: Mapping = ..., type: Union[type, Factory] = ...) -> None: ...
3434

3535
# NOTE: this overload for `attr` returns Any so that static analysis passes when used in the form: x : int = attr()
36-
# @overload
37-
# def attr(default: Any = ..., validator: Optional[Union[ValidatorType, List[ValidatorType]]] = ..., repr: bool = ..., cmp: bool = ..., hash: Optional[bool] = ..., init: bool = ..., convert: Optional[ConverterType] = ..., metadata: Mapping = ...) -> Any: ...
38-
# @overload
39-
def attr(default: Any = ..., validator: Optional[Union[ValidatorType, List[ValidatorType]]] = ..., repr: bool = ..., cmp: bool = ..., hash: Optional[bool] = ..., init: bool = ..., convert: Optional[ConverterType] = ..., metadata: Mapping = ..., type: Type[T] = ...) -> T: ...
36+
@overload
37+
def attr(default: Any = ..., validator: Optional[Union[_ValidatorType, List[_ValidatorType], Tuple[_ValidatorType, ...]]] = ..., repr: bool = ..., cmp: bool = ..., hash: Optional[bool] = ..., init: bool = ..., convert: Optional[_ConverterType] = ..., metadata: Mapping = ...) -> Any: ...
38+
@overload
39+
def attr(default: Any = ..., validator: Optional[Union[_ValidatorType, List[_ValidatorType], Tuple[_ValidatorType, ...]]] = ..., repr: bool = ..., cmp: bool = ..., hash: Optional[bool] = ..., init: bool = ..., convert: Optional[_ConverterType] = ..., metadata: Mapping = ..., type: Type[_T] = ...) -> _T: ...
4040

4141
@overload
4242
def attributes(maybe_cls: _C = ..., these: Optional[Dict[str, _CountingAttr]] = ..., repr_ns: Optional[str] = ..., repr: bool = ..., cmp: bool = ..., hash: Optional[bool] = ..., init: bool = ..., slots: bool = ..., frozen: bool = ..., str: bool = ...) -> _C: ...
4343
@overload
4444
def attributes(maybe_cls: None = ..., these: Optional[Dict[str, _CountingAttr]] = ..., repr_ns: Optional[str] = ..., repr: bool = ..., cmp: bool = ..., hash: Optional[bool] = ..., init: bool = ..., slots: bool = ..., frozen: bool = ..., str: bool = ...) -> Callable[[_C], _C]: ...
4545

4646
def fields(cls: type) -> Tuple[Attribute, ...]: ...
47-
def validate(inst: object) -> None: ...
47+
def validate(inst: Any) -> None: ...
4848

4949
def make_class(name, attrs: Union[List[_CountingAttr], Dict[str, _CountingAttr]], bases: Tuple[type, ...] = ..., **attributes_arguments) -> type: ...
5050

5151
# _funcs --
5252

5353
# FIXME: having problems assigning a default to the factory typevars
54-
# def asdict(inst: object, recurse: bool = ..., filter: Optional[FilterType] = ..., dict_factory: Type[_M] = dict, retain_collection_types: bool = ...) -> _M[str, Any]: ...
55-
# def astuple(inst: object, recurse: bool = ..., filter: Optional[FilterType] = ..., tuple_factory: Type[_I] = tuple, retain_collection_types: bool = ...) -> _I: ...
56-
def asdict(inst: object, recurse: bool = ..., filter: Optional[FilterType] = ..., dict_factory: Type[_M] = ..., retain_collection_types: bool = ...) -> _M: ...
57-
def astuple(inst: object, recurse: bool = ..., filter: Optional[FilterType] = ..., tuple_factory: Type[_I] = ..., retain_collection_types: bool = ...) -> _I: ...
54+
# def asdict(inst: Any, recurse: bool = ..., filter: Optional[_FilterType] = ..., dict_factory: Type[_M] = dict, retain_collection_types: bool = ...) -> _M[str, Any]: ...
55+
# def astuple(inst: Any, recurse: bool = ..., filter: Optional[_FilterType] = ..., tuple_factory: Type[_I] = tuple, retain_collection_types: bool = ...) -> _I: ...
56+
def asdict(inst: Any, recurse: bool = ..., filter: Optional[_FilterType] = ..., dict_factory: Type[_M] = ..., retain_collection_types: bool = ...) -> _M: ...
57+
def astuple(inst: Any, recurse: bool = ..., filter: Optional[_FilterType] = ..., tuple_factory: Type[_I] = ..., retain_collection_types: bool = ...) -> _I: ...
5858
def has(cls: type) -> bool: ...
5959
def assoc(inst: _T, **changes) -> _T: ...
6060
def evolve(inst: _T, **changes) -> _T: ...

src/attr/converters.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
from . import ConverterType
1+
from . import _ConverterType
22

3-
def optional(converter: ConverterType) -> ConverterType: ...
3+
def optional(converter: _ConverterType) -> _ConverterType: ...

src/attr/filters.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from typing import Union
2-
from . import Attribute, FilterType
2+
from . import Attribute, _FilterType
33

4-
def include(*what: Union[type, Attribute]) -> FilterType: ...
5-
def exclude(*what: Union[type, Attribute]) -> FilterType: ...
4+
def include(*what: Union[type, Attribute]) -> _FilterType: ...
5+
def exclude(*what: Union[type, Attribute]) -> _FilterType: ...

src/attr/validators.pyi

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from typing import Container, Iterable, List, Union
2-
from . import ValidatorType
2+
from . import _ValidatorType
33

4-
def instance_of(type: type) -> ValidatorType: ...
5-
def provides(interface) -> ValidatorType: ...
6-
def optional(validator: Union[ValidatorType, List[ValidatorType]]) -> ValidatorType: ...
7-
def in_(options: Container) -> ValidatorType: ...
8-
def and_(*validators: Iterable[ValidatorType]) -> ValidatorType: ...
4+
def instance_of(type: type) -> _ValidatorType: ...
5+
def provides(interface) -> _ValidatorType: ...
6+
def optional(validator: Union[_ValidatorType, List[_ValidatorType]]) -> _ValidatorType: ...
7+
def in_(options: Container) -> _ValidatorType: ...
8+
def and_(*validators: Iterable[_ValidatorType]) -> _ValidatorType: ...

0 commit comments

Comments
 (0)