-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Retain Sentinel types for non-CONSTANT_NAME bindings #11655
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4011,7 +4011,8 @@ export function createTypeEvaluator( | |
| if ( | ||
| TypeBase.isInstance(destType) && | ||
| !isConstantName(nameValue) && | ||
| !isFinalVariable(symbolWithScope.symbol) | ||
| !isFinalVariable(symbolWithScope.symbol) && | ||
| !isSentinelLiteral(destType) | ||
| ) { | ||
| destType = stripTypeForm(stripLiteralValue(destType)); | ||
| } | ||
|
|
@@ -24657,6 +24658,12 @@ export function createTypeEvaluator( | |
| isConstant = true; | ||
| } | ||
|
|
||
| // Sentinel objects are singletons; retaining the literal is sound | ||
| // even when the binding is not a CONSTANT_NAME or Final. | ||
| if (isSentinelLiteral(type)) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
📍 packages/pyright-internal/src/analyzer/typeEvaluator.ts:24658 [verified] |
||
| isConstant = true; | ||
| } | ||
|
|
||
| // If the symbol is constant, we can retain the literal | ||
| // value and TypeForm types. Otherwise, strip literal values | ||
| // and TypeForm types to widen. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| # This sample tests that Sentinel values retain their literal type | ||
| # even when the binding is not a CONSTANT_NAME. | ||
|
|
||
| from dataclasses import dataclass | ||
| from typing_extensions import Sentinel # pyright: ignore[reportMissingModuleSource] | ||
|
|
||
|
|
||
| Empty = Sentinel("Empty") | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
📍 packages/pyright-internal/src/tests/samples/sentinel3.py:8 [verified] |
||
| MISSING = Sentinel("MISSING") | ||
|
|
||
|
|
||
| def func1(value: int | Empty) -> None: | ||
| if value is Empty: | ||
| reveal_type(value, expected_text="Empty") | ||
| else: | ||
| reveal_type(value, expected_text="int") | ||
|
|
||
|
|
||
| def func2(value: int | Empty) -> None: | ||
| if value is not Empty: | ||
| reveal_type(value, expected_text="int") | ||
| else: | ||
| reveal_type(value, expected_text="Empty") | ||
|
|
||
|
|
||
| @dataclass | ||
| class Address: | ||
| email: str | ||
| name: str | None | ||
|
|
||
|
|
||
| def update_not_empty(address: Address, email: str | Empty, name: str | None | Empty) -> None: | ||
| if email is not Empty: | ||
| address.email = email | ||
| reveal_type(email, expected_text="str") | ||
| if name is not Empty: | ||
| address.name = name | ||
| reveal_type(name, expected_text="str | None") | ||
|
|
||
|
|
||
| def update_not_missing(address: Address, email: str | MISSING) -> None: | ||
| if email is not MISSING: | ||
| address.email = email | ||
| reveal_type(email, expected_text="str") | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new sample exercises module-level names but not the member-access widening path modified here. Add a class or instance sentinel-member regression case so this distinct retention path is covered.