Skip to content

Conversation

@terror
Copy link

@terror terror commented Dec 4, 2025

Resolves #1709

This diff validates type[...]/Type[...] arguments during annotation so typing.NewType aliases produce an
immediate invalid-annotation error instead of flowing through subtyping.

@meta-cla
Copy link

meta-cla bot commented Dec 4, 2025

Hi @terror!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@meta-cla
Copy link

meta-cla bot commented Dec 4, 2025

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@meta-cla meta-cla bot added the cla signed label Dec 4, 2025
from typing import NewType
Thing = NewType("Thing", int)
ThingType = type[Thing]
Copy link
Author

Choose a reason for hiding this comment

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

I'm wondering if we should also spend effort flagging invalid type[NewType] usages at annotation/alias definition time? Would be willing to follow up in a separate PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, we should add that check here:

// TODO: Validate that `targ` refers to a "valid in-scope class or TypeVar"

Copy link
Contributor

Choose a reason for hiding this comment

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

I think if we have a type[NewType] we may want to just emit the error at the definition site, and infer the type as Any.

Then we wouldn't need a custom error everywhere it's being used, so the error at the definition site would supersede this PR.

Copy link
Author

Choose a reason for hiding this comment

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

Yeah this makes more sense to me, I've re-purposed this PR to go down this route instead.

@yangdanny97
Copy link
Contributor

@migeed-z can you take this one?

@terror terror changed the title Add explicit error for using type[NewType] with NewType aliases Reject type[...] with NewType aliases at definition time Dec 4, 2025
@terror terror force-pushed the explicit-newtype-error branch from c3561b3 to 662e0d4 Compare December 4, 2025 21:47
@terror terror force-pushed the explicit-newtype-error branch from f9b8928 to 739d67f Compare December 4, 2025 22:00
ThingType = type[Thing] # E: NewType `Thing` is not a class and cannot be used with `type` or `Type`
OtherThingType = Type[Thing] # E: NewType `Thing` is not a class and cannot be used with `type` or `Type`
mapping: dict[int, ThingType] = {1: Thing} # E: `dict[int, type[Thing]]` is not assignable to `dict[int, type[Unknown]]`
Copy link
Author

Choose a reason for hiding this comment

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

note: I explicitly chose to keep cascading errors here. There may be a case for hiding them, but I think it makes sense to surface them 🤔

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.

type[NewType] in a dict leads to strange error

3 participants