Skip to content

Report a non-bool user-defined TYPE_CHECKING constant - #3846

Closed
markselby9 wants to merge 1 commit into
pyrefly-org:mainfrom
markselby9:feat/3756-typechecking-constant
Closed

markselby9 wants to merge 1 commit into
pyrefly-org:mainfrom
markselby9:feat/3756-typechecking-constant

Conversation

@markselby9

Copy link
Copy Markdown
Contributor

Closes #3756.

Summary

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.

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.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@markselby9
markselby9 force-pushed the feat/3756-typechecking-constant branch from e451215 to fdfd078 Compare June 17, 2026 10:17
@github-actions github-actions Bot added size/m and removed size/m labels Jun 17, 2026
@github-actions

This comment has been minimized.

@markselby9
markselby9 force-pushed the feat/3756-typechecking-constant branch from fdfd078 to 4ee029f Compare June 29, 2026 02:22
@github-actions github-actions Bot added size/m and removed size/m labels Jun 29, 2026
@github-actions

This comment has been minimized.

@yangdanny97

Copy link
Copy Markdown
Collaborator

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

@yangdanny97 yangdanny97 reopened this Jul 21, 2026
@github-actions github-actions Bot added size/m and removed size/m labels Jul 21, 2026
@markselby9

Copy link
Copy Markdown
Contributor Author

Sounds great, thanks @yangdanny97 — no worries about the backlog! The branch has drifted a fair bit behind main since; happy to rebase it if that helps the import, otherwise feel free to take it from here.

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
@markselby9
markselby9 force-pushed the feat/3756-typechecking-constant branch from 4ee029f to 5c045f3 Compare July 21, 2026 07:38
@github-actions github-actions Bot added size/m and removed size/m labels Jul 21, 2026
@markselby9

Copy link
Copy Markdown
Contributor Author

Done — rebased onto latest main, conflicts resolved and it's green/mergeable again. (The scope check is now derived from the existing is_in_function_scope + is_class_body_assignment binding fields, so the extra field my original diff added is gone.) Ready for import whenever it's convenient.

@github-actions

Copy link
Copy Markdown

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@meta-codesync

meta-codesync Bot commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

@yangdanny97 has imported this pull request. If you are a Meta employee, you can view this in D113084195.

@kinto0 kinto0 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Review automatically exported from Phabricator review in Meta.

@meta-codesync meta-codesync Bot closed this in c24e242 Jul 22, 2026
@meta-codesync meta-codesync Bot added the Merged label Jul 22, 2026
@meta-codesync

meta-codesync Bot commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

@yangdanny97 merged this pull request in c24e242.

avokin2 pushed a commit to avokin2/pyrefly that referenced this pull request Aug 12, 2026
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

new rule - invalid assignment to TYPE_CHECKING

3 participants