-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Add hidden --overwrite-union-syntax option #20332
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
This is peripherally relevant, but has there been previous discussions about the union syntax being rather ambiguous for def f1() -> int | None: return 1
def f2() -> int: return 1
reveal_type(f1) # Revealed type is "def () -> builtins.int | None"
reveal_type(f2 if int() else None) # Revealed type is "def () -> builtins.int | None" |
Though ultimately rejected, PEP 677 discusses that here: https://peps.python.org/pep-0677/#making-bind-tighter-than. |
We should probably consider adding parentheses to the second example. As a point of comparison, pyright infers it as |
This comment has been minimized.
This comment has been minimized.
I'd suggest we fix this after migrating the test output. That way would could immediately see the impact of a potential change. The fix should be a fairly simple change in the |
6227196 to
1b596ae
Compare
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
Followup to #20332 Change the test output from `Union` and `Optional` to the PEP 604 union syntax. This is already the default for `--python-version 3.10` and later if `--force-union-syntax` isn't used.
/CC @bzoracler I've opened #20406 to update the string output for PEP 604 unions with callables. |
Follow up to #20332 (comment) Wrap the callable with `( )` if it's within a union to reduce ambiguity. This matches the pyright behavior. ```py def f1() -> int | None: return 1 def f2() -> int: return 1 reveal_type(f1) # def () -> builtins.int | None reveal_type(f2 if int() else None) # (def () -> builtins.int)) | None ```
Start updating the test output (errors and reveal type) to use the PEP 604 union syntax. The process is similar to what we did last year when we adopted PEP 585 generics in the test output.
As we still default to Python 3.9 for type checking, this PR adds a temporary internal flag
--overwrite-union-syntaxto be able to overwrite the output.This PR only changes a handful of test files so the modifications to mypy itself are clearly visible. The goal is to change the test output for all other tests in followup PRs. Once that's done, I plan to deprecate the
--force-union-syntaxflag similar to the already deprecated--force-uppercase-builtins.