You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If I try to pass a TypeVar bound to a union to a function that overloads for both part of the union, I get a [arg-type] error. But if I pass the same union not as a TypeVar, it works.
Note that pyright has similar behaviour here.
To Reproduce
importosfromtypingimportTypeVar, Unionfromtyping_extensionsimportTypeAlias, reveal_type_StrPathT=TypeVar("_StrPathT", bound=Union[str, os.PathLike[str]])
_StrPath: TypeAlias=Union[str, os.PathLike[str]]
deffoo(file_path: _StrPathT) ->_StrPathT:
# Mypy:# Argument 1 to "dirname" has incompatible type "_StrPathT"; expected "PathLike[str]" [arg-type]# Revealed type is "builtins.str"# Pyright:# No overloads for "dirname" match the provided arguments [reportCallIssue]# posixpath.pyi(88, 5): Overload 2 is the closest match# Argument of type "_StrPathT@foo" cannot be assigned to parameter "p" of type "AnyOrLiteralStr@dirname" in function "dirname"# Type "_StrPathT@foo" is not assignable to constrained type variable "AnyOrLiteralStr" [reportArgumentType]# Type of "os.path.dirname(file_path)" is "Unknown"reveal_type(os.path.dirname(file_path))
returnfile_pathdefbar(file_path: _StrPath) ->_StrPath:
# OK, both reveal type as strreveal_type(os.path.dirname(file_path))
returnfile_path
Expected Behavior
This feels like a bug but I'm not completely certain. If I split the possible types _StrPathT = TypeVar("_StrPathT", str, os.PathLike[str]) it works, but then my method becomes incorrectly typed as it no longer accepts a str | os.PathLike[str]
Actual Behavior
See comments in MRE
Your Environment
Mypy version used: mypy 1.13.0 (compiled: yes)
Mypy command-line flags: N/A
Mypy configuration options from mypy.ini (and other config files): (I've replicated this issue with an empty config file)
Python version used: 3.9.13
(I searched for similar issues and only found #13596)
The text was updated successfully, but these errors were encountered:
main.py:28: error: Incompatible return value type (got "int", expected "T") [return-value]
main.py:28: error: Argument 1 to "fun" has incompatible type "T"; expected "int" [arg-type]
Found 2 errors in 1 file (checked 1 source file)
In our case we were able to avoid the problem by switching from approach 3 to approach 1. But using the union as a bound as in example_3 seems intuitive and sufficient. Developers in our team were stumped by the error.
Bug Report
If I try to pass a TypeVar bound to a union to a function that overloads for both part of the union, I get a [arg-type] error. But if I pass the same union not as a TypeVar, it works.
Note that pyright has similar behaviour here.
To Reproduce
Expected Behavior
This feels like a bug but I'm not completely certain. If I split the possible types
_StrPathT = TypeVar("_StrPathT", str, os.PathLike[str])
it works, but then my method becomes incorrectly typed as it no longer accepts astr | os.PathLike[str]
Actual Behavior
See comments in MRE
Your Environment
mypy.ini
(and other config files): (I've replicated this issue with an empty config file)(I searched for similar issues and only found #13596)
The text was updated successfully, but these errors were encountered: