Skip to content

Conversation

@dlax
Copy link
Contributor

@dlax dlax commented Apr 30, 2021

Description

Fixes #11004

The following case defines a function with both an explicit keyword argument and **kwargs but with types incompatible (int for the explicit one, str for **kwargs):

from typing import Optional, overload

@overload
def f(x: int = 0, **kwargs: str) -> None:
    ...
@overload
def f(x: int = 0, *, y: int, **kwargs: str) -> str:
    ...
def f(x: int = 0, *, y: Optional[int] = None, **kwargs: str) -> Optional[str]:
# E: Overloaded function implementation does not accept all possible arguments of signature 1
    ...

reveal_type(f(0))
# N: Revealed type is "None"
reveal_type(f(1, foo='bar'))
# N: Revealed type is "None"
reveal_type(f(1, y=2, foo='bar'))
# N: Revealed type is "builtins.str"

Mypy raises an error "Overloaded function implementation does not accept all possible arguments of signature 1" but types are correctly inferred, so the error seems to be a false-positive.

Allowing partial overlap when checking compatibility between one overload alternative and the implementation fixes the issue (the error is no longer raised). Calling is_callable_compatible(..., allow_partial_overlap=True) is documented to "return True if there exists at least one call to left that's also a call to right"; which seems valid for overloads.

(This appears so simple that it's quite possible that I missed something...)

Test Plan

Example above is included as a test case. No apparent breakage in the test suite.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@adam-grant-hendry
Copy link

@dlax Is this planned to be merged soon?

@dlax dlax force-pushed the overload-kwargs branch from 19da938 to 5873910 Compare March 8, 2024 09:50
@dlax dlax force-pushed the overload-kwargs branch from 5873910 to c5a1670 Compare March 8, 2024 09:51
@github-actions
Copy link
Contributor

github-actions bot commented Mar 8, 2024

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

steam.py (https://github.com/Gobot1234/steam.py)
- steam/utils.py:539: error: Overloaded function implementation does not accept all possible arguments of signature 2  [misc]
+ steam/ext/commands/commands.py:802: error: Unused "type: ignore" comment  [unused-ignore]
- steam/ext/commands/commands.py:851: error: Overloaded function implementation does not accept all possible arguments of signature 3  [misc]
- steam/ext/commands/cog.py:53: error: Overloaded function implementation does not accept all possible arguments of signature 1  [misc]

jinja (https://github.com/pallets/jinja)
+ src/jinja2/filters.py:1368: error: Overloaded function implementation does not accept all possible arguments of signature 2  [misc]

tornado (https://github.com/tornadoweb/tornado)
+ tornado/gen.py:177: error: Overloaded function implementation does not accept all possible arguments of signature 2  [misc]

spark (https://github.com/apache/spark)
+ python/pyspark/rdd.py:4304: error: Unused "type: ignore" comment  [unused-ignore]
+ python/pyspark/sql/dataframe.py:1908: error: Unused "type: ignore" comment  [unused-ignore]
+ python/pyspark/sql/dataframe.py:2041: error: Unused "type: ignore" comment  [unused-ignore]
+ python/pyspark/sql/dataframe.py:6414: error: Unused "type: ignore" comment  [unused-ignore]
+ python/pyspark/sql/connect/dataframe.py:298: error: Unused "type: ignore" comment  [unused-ignore]
+ python/pyspark/sql/connect/dataframe.py:345: error: Unused "type: ignore" comment  [unused-ignore]

hydra-zen (https://github.com/mit-ll-responsible-ai/hydra-zen)
- src/hydra_zen/typing/_builds_overloads.py:238: error: Overloaded function implementation does not accept all possible arguments of signature 3  [misc]
+ src/hydra_zen/typing/_builds_overloads.py:238: error: Overloaded function implementation does not accept all possible arguments of signature 2  [misc]
- src/hydra_zen/typing/_builds_overloads.py:238: error: Overloaded function implementation does not accept all possible arguments of signature 5  [misc]
- src/hydra_zen/typing/_builds_overloads.py:238: error: Overloaded function implementation does not accept all possible arguments of signature 7  [misc]
- src/hydra_zen/typing/_builds_overloads.py:472: error: Overloaded function implementation does not accept all possible arguments of signature 2  [misc]
+ src/hydra_zen/typing/_builds_overloads.py:472: error: Overloaded function implementation does not accept all possible arguments of signature 3  [misc]
- src/hydra_zen/typing/_builds_overloads.py:472: error: Overloaded function implementation does not accept all possible arguments of signature 4  [misc]
- src/hydra_zen/typing/_builds_overloads.py:472: error: Overloaded function implementation does not accept all possible arguments of signature 6  [misc]
- src/hydra_zen/typing/_builds_overloads.py:472: error: Overloaded function implementation does not accept all possible arguments of signature 8  [misc]
- src/hydra_zen/typing/_builds_overloads.py:657: error: Overloaded function implementation does not accept all possible arguments of signature 1  [misc]
- src/hydra_zen/typing/_builds_overloads.py:657: error: Overloaded function implementation does not accept all possible arguments of signature 5  [misc]
+ src/hydra_zen/typing/_builds_overloads.py:657: error: Overloaded function implementation does not accept all possible arguments of signature 4  [misc]
+ src/hydra_zen/typing/_builds_overloads.py:657: error: Overloaded function implementation does not accept all possible arguments of signature 7  [misc]

@dlax
Copy link
Contributor Author

dlax commented Mar 8, 2024

Rebased and resolved conflicts.

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.

Wrong "Overloaded function implementation does not accept all possible arguments"

2 participants