Skip to content

Suggest valid type forms when type(...) is called in annotations - #5120

Open
QEDady wants to merge 1 commit into
facebook:mainfrom
QEDady:suggest-not-implemented-type-in-annotations
Open

QEDady wants to merge 1 commit into
facebook:mainfrom
QEDady:suggest-not-implemented-type-in-annotations

Conversation

@QEDady

@QEDady QEDady commented Oct 6, 2026

Copy link
Copy Markdown
Collaborator

Summary

Follow-up to #5087.

In Python, built-in singleton constants and runtime objects like NotImplemented, Ellipsis, None, and modules do not expose their types as builtins, so users frequently write type(...) in type annotations (or confuse type(MyClass) with type[MyClass]).

When a single-argument call to builtins.type(...) appears in an annotation where function calls are invalid, inspect the argument type and append a helpful suggestion to the invalid-annotation error message:

  • type(NotImplemented) -> Did you mean `types.NotImplementedType`?
  • type(Ellipsis) / type(...) -> Did you mean `types.EllipsisType`?
  • type(None) -> Did you mean `None`?
  • type(some_module) -> Did you mean `types.ModuleType`?
  • type(MyClass) -> Did you mean `type[MyClass]`?

Test Plan

Added test_type_call_in_annotations in pyrefly/lib/test/annotation.rs and ran cargo test.

@meta-cla meta-cla Bot added the cla signed label Oct 6, 2026
@QEDady QEDady self-assigned this Oct 6, 2026
@github-actions github-actions Bot added google issues from google size/s labels Oct 6, 2026
@QEDady
QEDady requested a review from rchen152 October 6, 2026 20:53
@meta-codesync

meta-codesync Bot commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

This pull request has been imported. If you are a Meta employee, you can view this in D123716933. (Because this pull request was imported automatically, there will not be any future comments.)

@github-actions

github-actions Bot commented Oct 6, 2026

Copy link
Copy Markdown

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

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant