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
From its docsos.fspath can take any os.PathLike object but must return either str or bytes. However, typeshed appears to only support str, bytes, or PathLike[AnyStr] (from here). Would a new overload be appropriate here? Like:
deffspath(path: PathLike[Any]) ->AnyStr: ...
Example:
importosdefget_path(path: object) ->str:
ifisinstance(path, os.PathLike):
path_repr=os.fspath(path)
reveal_type(path_repr)
ifisinstance(path_repr, bytes):
returnpath_repr.decode("UTF-8")
else:
returnpath_reprelse:
raiseException("I don't know what to do with this")
$ mypy --warn-return-any --custom-typeshed-dir ~/src/typeshed/ type.py
type.py:6: note: Revealed type is 'Any'
type.py:10: error: Returning Any from function declared to return "str"
Found 1 error in 1 file (checked 1 source file)
(I understand mypy != typeshed, but wasn't sure of a better way to give an example)
I'm not terribly well versed with typeshed/the deeper details of typing in general, so apologies if I'm completely misunderstanding anything.
The text was updated successfully, but these errors were encountered:
Adding your overload doesn't change the result of type checking for me, does it for you? (Asking because I see you used --custom-typeshed-dir in your command)
This is a case where gradual typing / "Any turns the type checker off" is suboptimal for some use cases. E.g., when matching def fspath(path: PathLike[AnyStr]) -> AnyStr: ... to PathLike[Any], mypy could probably infer the return type as Union[str, bytes], based on the value restriction of the AnyStr typevar, but instead it propagates the Any.
This is great for gradual typing, but it looks like in your case you'd prefer the Union return that you can pick apart at your leisure. (A bit of an in between would be if we added something like python/typing#566, mypy could use those kind of semantics for it. In fact, some people have suggested using a value restricted typevar as a way to spell this).
I don't think there's any overload typeshed could add that would do what you want (and I'm not sure we'd accept it if there was one), so I'm closing. Note a workaround for your mypy error is to add an if isinstance(path_repr, str) before your second return (and throw an exception otherwise).
Adding your overload doesn't change the result of type checking for me, does it for you?
Ah, my quick and dirty test was actually to change the existing overload (which I imagine is far from acceptable). If I add the overload instead I see the same result as you: result of type checking is unchanged.
From its docs
os.fspath
can take anyos.PathLike
object but must return eitherstr
orbytes
. However, typeshed appears to only supportstr
,bytes
, orPathLike[AnyStr]
(from here). Would a new overload be appropriate here? Like:Example:
(I understand
mypy
!= typeshed, but wasn't sure of a better way to give an example)I'm not terribly well versed with typeshed/the deeper details of typing in general, so apologies if I'm completely misunderstanding anything.
The text was updated successfully, but these errors were encountered: