fix(stubgen): emit from typing import Literal when Literal annotations are added#4144
Closed
tobyh-canva wants to merge 1 commit into
Closed
fix(stubgen): emit from typing import Literal when Literal annotations are added#4144tobyh-canva wants to merge 1 commit into
from typing import Literal when Literal annotations are added#4144tobyh-canva wants to merge 1 commit into
Conversation
tobyh-canva
commented
Jul 14, 2026
Comment on lines
-61
to
-67
| pub uses_self: bool, | ||
| /// Whether any item renders a `Callable[...]` annotation (so we know | ||
| /// whether to emit `from typing import Callable`). | ||
| pub uses_callable: bool, | ||
| /// Whether any item renders a `ClassVar[...]` annotation (so we know | ||
| /// whether to emit `from typing import ClassVar`). | ||
| pub uses_classvar: bool, |
Collaborator
Author
There was a problem hiding this comment.
Since this PR would have required me adding another bool uses_literal, I figured I might as well take this opportunity to consolidate all these bools into a set typing_imports.
But please do let me know if the bools are preferred
tobyh-canva
force-pushed
the
fix-stubgen-missing-literal-import
branch
from
July 14, 2026 12:23
0d0fd44 to
e18129a
Compare
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
Contributor
|
@yangdanny97 has imported this pull request. If you are a Meta employee, you can view this in D111931494. |
stroxler
approved these changes
Jul 14, 2026
stroxler
left a comment
Contributor
There was a problem hiding this comment.
Review automatically exported from Phabricator review in Meta.
Contributor
|
@yangdanny97 merged this pull request in 3213839. |
meta-codesync Bot
pushed a commit
that referenced
this pull request
Jul 16, 2026
…import (#4173) Summary: ## Problem `stubgen::tests::test_stubgen_type_checking_or_guard` panics on `main`. The `type_checking_or_guard` snapshot referenced `Literal[True]` **without importing `Literal`** (an invalid stub). Once #4144 made stubgen emit `from typing import Literal` when it adds a `Literal[...]` annotation, this fixture went stale and the snapshot assertion started failing. ## Fix Regenerate the `type_checking_or_guard` `expected.pyi` to include the now-emitted `from typing import Literal`, keeping the `# generated` header used by the other fixtures. This is a pure test-fixture update — no stubgen logic change. ## Testing `cargo test --lib -p pyrefly stubgen::` → all 24 stubgen tests pass (was 1 failing). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Pull Request resolved: #4173 Reviewed By: kinto0 Differential Revision: D112434885 Pulled By: samwgoldman fbshipit-source-id: fe303f4b35600c990281512430d3a5c6c925599c
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.
Context
Currently, stubgen emits inferred
Literalannotations, but forgets to addfrom typing import Literalat the top of the file.Additionally, as shown by the existing
class_varssnapshot test, enum members are currently incorrectly annotated withLiteral, whereas the typing spec says enum members should not be annotated at all (ref).Changes
typingimports in stubgen