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
fromtypingimportUniondeffoo(value: int|str=None) ->None: # error: Incompatible default for argument "value" (default has type "None", argument has type "Union[int, str]")
...
defbar(value: Union[int, str] =None) ->None: # no error
...
I asked the same question on stackoverflow a little while back (and neglected to issue a bug report until today). PEP 484 indicates that the second example is the correct way to write it, as Optional should be explicit.
Implicit optional is supported for backward compatibility, but its use is deprecated as per PEP 484, which states:
This is no longer the recommended behavior. Type checkers should move towards requiring the optional type to be made explicit.
Since the | union syntax is new, there is no backward-compatibility issue with enforcing the explicit use of None. I suspect that it was therefore intentional not to support the old implicit optional behavior with the new syntax.
You can avoid the error by explicitly including None in the union:
I'm not sure this is really intentional, but I agree with @erictraut that we should keep the behavior as is. Implicit optional is a deprecated behavior that we're phasing out; we shouldn't introduce it with new syntax.
https://mypy-play.net/?mypy=latest&python=3.10&gist=13ab271a59d6301d98dac48ccb87e9c8
The text was updated successfully, but these errors were encountered: