Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 6, 2018

Conversation

euresti
Copy link
Contributor

@euresti euresti commented Feb 28, 2018

This makes the following not give a type error because of python/mypy#4554

   y: DefaultDict[str, List[int]] = Factory(lambda: defaultdict(list))

This makes the following not give a type error:

   y: DefaultDict[str, List[int]] = Factory(lambda: defaultdict(list))
@euresti
Copy link
Contributor Author

euresti commented Feb 28, 2018

@chadrik Another change for you. :)

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]) -> _T: ...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this equivalent to the second overload? If not why, don't you need one where the Callable takes an argument and the second argument is omitted?

Copy link
Contributor Author

@euresti euresti Feb 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't set takes_self then you'd better not send in a function with an argument (because the default value is False). If you do set takes_self then both are allowed.

That being said I'm ok with switching this to:

@overload
def Factory(factory: Callable[[], _T], takes_self: bool = ...) -> _T: ...
@overload
def Factory(factory: Callable[[Any], _T], takes_self: bool = ...) -> _T: ...

And forget about the first overload.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the first overload is redundant because the argument has a default.

@euresti
Copy link
Contributor Author

euresti commented Feb 28, 2018

First overload removed.

@JelleZijlstra JelleZijlstra merged commit 50c7188 into python:master Mar 6, 2018
yedpodtrzitko pushed a commit to yedpodtrzitko/typeshed that referenced this pull request Jan 23, 2019
This makes the following not give a type error:

   y: DefaultDict[str, List[int]] = Factory(lambda: defaultdict(list))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants