Work around mypy issue #4554 in attr.Factory (#1933)

This makes the following not give a type error:

   y: DefaultDict[str, List[int]] = Factory(lambda: defaultdict(list))
This commit is contained in:
David Euresti
2018-03-05 19:21:14 -08:00
committed by Jelle Zijlstra
parent 1c47458ac6
commit 50c7188300

View File

@@ -21,7 +21,11 @@ _ValidatorArgType = Union[_ValidatorType[_T], Sequence[_ValidatorType[_T]]]
NOTHING: object
# NOTE: Factory lies about its return type to make this possible: `x: List[int] = Factory(list)`
def Factory(factory: Union[Callable[[], _T], Callable[[Any], _T]], takes_self: bool = ...) -> _T: ...
# Work around mypy issue #4554 by using an overload vs a Union.
@overload
def Factory(factory: Callable[[], _T], takes_self: bool = ...) -> _T: ...
@overload
def Factory(factory: Callable[[Any], _T], takes_self: bool = ...) -> _T: ...
class Attribute(Generic[_T]):
name: str