Repository navigation
Report a non-bool user-defined TYPE_CHECKING constant - #3846
markselby9 wants to merge 1 commit into
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
e451215 to
fdfd078
Compare
This comment has been minimized.
This comment has been minimized.
fdfd078 to
4ee029f
Compare
This comment has been minimized.
This comment has been minimized.
|
Hi @markselby9 sorry we didn't get to this one yet, there's a fairly large backlog of PRs. If you don't mind I'd like to reopen this and get it merged |
|
Sounds great, thanks @yangdanny97 — no worries about the backlog! The branch has drifted a fair bit behind |
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 pyrefly-org#3756
4ee029f to
5c045f3
Compare
|
Done — rebased onto latest |
|
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 D113084195. |
kinto0
left a comment
There was a problem hiding this comment.
Review automatically exported from Phabricator review in Meta.
|
@yangdanny97 merged this pull request in c24e242. |
…g#3846) Summary: Closes pyrefly-org#3756. A user-defined module-level `TYPE_CHECKING` constant is treated as `True` by type checkers and `False` at runtime, so it must be a `bool` (conventionally `TYPE_CHECKING = False`). A definition like `TYPE_CHECKING: str = ""` is internally consistent yet wrong, and was previously accepted silently. This mirrors ty's `invalid-type-checking-constant`. When solving a module-scope name assignment to a type-checking constant (`TYPE_CHECKING` or `TYPE_CHECKING_WITH_PYREFLY`), this checks that its type is assignable to `bool` and otherwise emits a new `invalid-type-checking-constant` error. Imports of `typing.TYPE_CHECKING`, function locals, and class attributes that merely share the name are unaffected. Pull Request resolved: pyrefly-org#3846 Test Plan: New tests in `pyrefly/lib/test/sys_info.rs`: `TYPE_CHECKING: str = ""` / `= 1` (and `TYPE_CHECKING_WITH_PYREFLY`) are flagged; the canonical import + `if TYPE_CHECKING:`, `= False`, `: bool = False`, class attributes, and function locals are not. `cargo test` passes; full lib test suite is green. Reviewed By: kinto0 Differential Revision: D113084195 Pulled By: yangdanny97 fbshipit-source-id: eef021fa5c9238866f47abd8658923b4f44f811e
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.