Skip to content

Make optional type narrowing actually optional in tuples_type_compat.py #1981

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

Merged

Conversation

missingdays
Copy link
Contributor

This pull request marks optional type narrowing in tuples_type_compat.py with E? to signify its optionality. Otherwise, it is treated as obligatory.

See the comment NOTE: This type narrowing functionality is optional, not mandated. at the beginning of each function.

@python-cla-bot
Copy link

python-cla-bot bot commented Apr 22, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

@erictraut
Copy link
Collaborator

I agree these should be marked with "E?" rather than "E" because they are optional in the spec. Thanks for the fix.

@erictraut erictraut merged commit c55a4d0 into python:main Apr 22, 2025
4 checks passed
@@ -71,15 +71,15 @@ def func4(
def func5(val: tuple[int] | tuple[str, str] | tuple[int, *tuple[str, ...], int]):
if len(val) == 1:
# Type can be narrowed to tuple[int].
assert_type(val, tuple[int]) # tuple[int]
assert_type(val, tuple[int]) # E?: tuple[int]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes the test not very useful (without manual review) since type checkers will now pass no matter what type they infer for val. One solution could be to use two types and asserts that exactly one of them errors, by using an error group and asserting for both the narrowed and non-narrowed type. However, we don't currently check in that case that there is exactly one error. I can send a PR doing that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds reasonable to me.

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.

3 participants