Skip to content

fix(stubgen): emit from typing import Literal when Literal annotations are added#4144

Closed
tobyh-canva wants to merge 1 commit into
facebook:mainfrom
tobyh-canva:fix-stubgen-missing-literal-import
Closed

fix(stubgen): emit from typing import Literal when Literal annotations are added#4144
tobyh-canva wants to merge 1 commit into
facebook:mainfrom
tobyh-canva:fix-stubgen-missing-literal-import

Conversation

@tobyh-canva

@tobyh-canva tobyh-canva commented Jul 14, 2026

Copy link
Copy Markdown
Collaborator

Context

Currently, stubgen emits inferred Literal annotations, but forgets to add from typing import Literal at the top of the file.

Additionally, as shown by the existing class_vars snapshot test, enum members are currently incorrectly annotated with Literal, whereas the typing spec says enum members should not be annotated at all (ref).

Changes

  • Add snapshot test demonstrating the aforementioned bugs
  • Fix the bugs
  • Perform a minor code cleanup regarding typing imports in stubgen

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,

@tobyh-canva tobyh-canva Jul 14, 2026

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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
tobyh-canva force-pushed the fix-stubgen-missing-literal-import branch from 0d0fd44 to e18129a Compare July 14, 2026 12:23
@github-actions github-actions Bot added size/s and removed size/s labels Jul 14, 2026
@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 14, 2026

Copy link
Copy Markdown
Contributor

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

@stroxler stroxler 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 3213839 Jul 14, 2026
@meta-codesync meta-codesync Bot added the Merged label Jul 14, 2026
@meta-codesync

meta-codesync Bot commented Jul 14, 2026

Copy link
Copy Markdown
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
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.

3 participants