Skip to content

(🎁) conformance: NewType isn't a class #2015

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

KotlinIsland
Copy link
Contributor

@KotlinIsland KotlinIsland commented May 28, 2025

i can't update the results on my machine, sorry!

@KotlinIsland KotlinIsland changed the title conformance: Newtype isn't a class (🎁) conformance: Newtype isn't a class May 28, 2025
@@ -14,6 +14,8 @@

assert_type(UserId(5) + 1, int)

UserId.mro() # E: NewType is not an instance of `type`
Copy link
Collaborator

Choose a reason for hiding this comment

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

By my reading of the spec, this shouldn't result in a type error. Can you point to the text in the spec that would indicate that it should? All conformance tests should be motivated by the language in the typing spec. If you think that there's a hole in the typing spec, please work through the process to change the spec.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

By my reading of the spec, this shouldn't result in a type error.

where in the spec does it say that the return value of NewType is an instance of type?

While at runtime, NewType('Derived', Base) returns a dummy function that simply returns its argument

The function returned by NewType

since function objects don’t support these operations.

seems pretty clear to me that it returns a function

Copy link
Collaborator

Choose a reason for hiding this comment

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

That's a reasonable point. I think this is somewhat of a gray area, but it is implied by the current wording in the typing spec even though it only explicitly mentions "Both isinstance and issubclass, as well as subclassing will fail...".

@JelleZijlstra, what do you think about including this?

@KotlinIsland, in general, we want to make sure that the conformance tests are well motivated by the text of the spec, but there are admittedly some gray areas where the spec implies something without stating it. You'll see that most tests actually include a quote from the spec so it's clear what part they're testing.

Copy link
Contributor Author

@KotlinIsland KotlinIsland May 28, 2025

Choose a reason for hiding this comment

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

at runtime it is a function, as the spec says it must be. so i don't see how this is grey at all

i think it's clear that the spec is covering isinstance and subclassing to rule out the idea that is could be a type, not to subtly imply that it is a type

Copy link
Contributor Author

@KotlinIsland KotlinIsland May 28, 2025

Choose a reason for hiding this comment

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

i think it would benefit the spec for type statments to include similar wording, such to specify that type statements will return a TypeAliasType, not a type, and they they are not suported by isinstance and subtyping

Copy link
Member

Choose a reason for hiding this comment

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

I'd prefer for the spec to not go into too much detail about how things are implemented at runtime; that can change between Python versions and keeping the details up to date in the spec doesn't seem useful.

I wouldn't be opposed to saying that a NewType isn't an instance of type, though. We could also add a test enforcing the same thing. I'd prefer to test it with something like _: type = UserId instead of relying on the .mro method.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

great, this has been updated and pushed

@KotlinIsland KotlinIsland changed the title (🎁) conformance: Newtype isn't a class (🎁) conformance: NewType isn't a class May 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants