Skip to content

Support type objects in functools.partial - #17292

Merged
hauntsaninja merged 2 commits into
python:masterfrom
hauntsaninja:functools-partial
May 25, 2024
Merged

hauntsaninja merged 2 commits into
python:masterfrom
hauntsaninja:functools-partial

Conversation

@hauntsaninja

Copy link
Copy Markdown
Collaborator

No description provided.

@hauntsaninja
hauntsaninja requested a review from cdce8p May 25, 2024 05:09
@github-actions

This comment has been minimized.

@github-actions

Copy link
Copy Markdown
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

websockets (https://github.com/aaugustin/websockets)
- src/websockets/legacy/server.py:1048: error: "type[WebSocketServerProtocol]" not callable  [misc]
- src/websockets/legacy/client.py:496: error: "type[WebSocketClientProtocol]" not callable  [misc]

isort (https://github.com/pycqa/isort)
- isort/exceptions.py:13: error: "Type[ISortError]" not callable  [misc]

@cdce8p cdce8p left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks @hauntsaninja 👍🏻

Comment on lines -306 to +311
p = functools.partial(t, 1) # E: "Type[A]" not callable
p = functools.partial(t, 1)
reveal_type(p) # N: Revealed type is "functools.partial[__main__.A]"

p("a") # OK
p(1) # False negative
p(z=1) # False negative
p(1) # E: Argument 1 to "A" has incompatible type "int"; expected "str"
p(z=1) # E: Unexpected keyword argument "z" for "A"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Awesome that these are now fixed!

@cdce8p cdce8p added the upnext label May 25, 2024
@hauntsaninja
hauntsaninja merged commit 9315d62 into python:master May 25, 2024
@hauntsaninja
hauntsaninja deleted the functools-partial branch May 26, 2024 00:45
@tamird

tamird commented May 29, 2024

Copy link
Copy Markdown
Contributor

This is great, thanks @hauntsaninja! Looks like there's still a bug in the presence of a Callable[[Type[T]], T]; see #17297 for a repro case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

3 participants