Skip to content

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

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

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
Contributor

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
Contributor 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.

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.

1 participant