Fix missing error for "str" | None union with string literals#2828
Open
fangyi-zhou wants to merge 8 commits intofacebook:mainfrom
Open
Fix missing error for "str" | None union with string literals#2828fangyi-zhou wants to merge 8 commits intofacebook:mainfrom
"str" | None union with string literals#2828fangyi-zhou wants to merge 8 commits intofacebook:mainfrom
Conversation
`NoneType` is a plain type at runtime that doesn't support `__or__` with string arguments, so `"Foo" | None` raises a TypeError just like `"Foo" | int`. The `is_plain_type` check was missing the `Type::None` case. Fixes facebook#1005 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This comment has been minimized.
This comment has been minimized.
Add test cases covering more Type variants (Any, builtin parameterized generics) and a runtime validation script for reviewers. The Any case was already handled correctly. Builtin parameterized generics like list[int] are a known false negative documented with a bug marker. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…ble, LiteralString TypedDict, NamedTuple, and Protocol are class definitions that correctly get flagged. Literal[1], Callable[[int], str], and LiteralString are typing special forms that correctly pass through. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…, LiteralString, Type[Any], Never, NoReturn Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Moved to a gist for PR reviewers instead. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
|
@yangdanny97 has imported this pull request. If you are a Meta employee, you can view this in D97631848. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
At runtime,
"Foo" | NoneraisesTypeErrorbecauseNoneTypedoesn't support__or__with string operands. Pyrefly'sis_plain_typecheck was missingType::None, so this case wasn't flagged.Fix: Add
Type::None => truetois_plain_typein theForwardRefUnioncheck.Tests: Add cases covering
None,Any,TypedDict,NamedTuple,Protocol,Literal[1],Callable,LiteralString, and builtin parameterized generics (list[int],dict[str, int]).Known gap: Builtin parameterized generics like
list[int](types.GenericAlias) error at runtime butList[int](typing._GenericAlias) does not. Pyrefly represents both identically, so it can't distinguish them. This is documented with abugmarker.Runtime validation script: https://gist.github.com/fangyi-zhou/ce153247db2e6b71e985f27e9e31c03b
Fixes #1005
Test plan
cargo test -p pyrefly test_union— all 44 tests passpython3 test.py --no-test --no-conformance— formatting and linting clean🤖 Generated with Claude Code