Report a non-bool user-defined TYPE_CHECKING constant#3846
Open
markselby9 wants to merge 1 commit into
Open
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Type checkers treat `TYPE_CHECKING` as `True` while the runtime sees `False`, so a user-defined module-level `TYPE_CHECKING` (or pyrefly's `TYPE_CHECKING_WITH_PYREFLY`) constant must be a `bool` (conventionally `TYPE_CHECKING = False`). A definition like `TYPE_CHECKING: str = ""` is internally consistent yet wrong, and was previously accepted silently. When solving a module-scope name assignment to a type-checking constant, check that its type is assignable to `bool`, otherwise emit the new `invalid-type-checking-constant` error. The check is restricted to module scope (tracked via a new `NameAssign::is_module_scope` flag) and skips stub files, so imports of `typing.TYPE_CHECKING`, function locals, class attributes that merely share the name, and `.pyi` placeholder values are unaffected. Closes facebook#3756
e451215 to
fdfd078
Compare
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
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.
Closes #3756.
Summary
A user-defined module-level
TYPE_CHECKINGconstant is treated asTrueby type checkers andFalseat runtime, so it must be abool(conventionallyTYPE_CHECKING = False). A definition likeTYPE_CHECKING: str = ""is internally consistent yet wrong, and was previously accepted silently. This mirrors ty'sinvalid-type-checking-constant.When solving a module-scope name assignment to a type-checking constant (
TYPE_CHECKINGorTYPE_CHECKING_WITH_PYREFLY), this checks that its type is assignable tobooland otherwise emits a newinvalid-type-checking-constanterror. Imports oftyping.TYPE_CHECKING, function locals, and class attributes that merely share the name are unaffected.Test Plan
New tests in
pyrefly/lib/test/sys_info.rs:TYPE_CHECKING: str = ""/= 1(andTYPE_CHECKING_WITH_PYREFLY) are flagged; the canonical import +if TYPE_CHECKING:,= False,: bool = False, class attributes, and function locals are not.cargo testpasses; full lib test suite is green.