Skip to content

Conversation

@hauntsaninja
Copy link
Collaborator

No description provided.

@hauntsaninja hauntsaninja marked this pull request as ready for review January 12, 2024 05:53
concat(m, b) # Type error

# TODO: should these be str or Any?
# reveal_type(concat(m, a))
Copy link
Collaborator

Choose a reason for hiding this comment

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

This gets into constraint-solving behavior which is underspecified, but I'm still surprised to see that mypy generates an Any in this case. That's not what I would expect, and it seems inconsistent with mypy's handling of non-constrained TypeVars.

class GenericMeta(type, Generic[T]): ...

class GenericMetaInstance(metaclass=GenericMeta[T]): # Type error
...
Copy link
Collaborator

Choose a reason for hiding this comment

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

It may also be useful to add tests for accessing a TypeVar as a runtime object — to make sure that this isn't disallowed.

assert_type(T.__name__, str)
assert_type(T.__bound__, Any | None)
T.other # Type error

runtime_typevar: TypeVar = T # OK

@hauntsaninja
Copy link
Collaborator Author

hauntsaninja commented Jan 14, 2024

@erictraut I don't think we expect any "derives from protocol but doesn't implement all methods" errors here, or at least I don't see any.

On the following file pyright 1.1.345 has no complaints:

from typing import TypeVar
from collections.abc import Iterator
T = TypeVar('T')
class MyIter1(Iterator[T]): ...
MyIter1()

mypy, pyre and pytype are all fine with the class definition, but treat it as an abstract class and error when you attempt to instantiate.

I can still add the missing methods if you want, but wanted to make sure I wasn't missing something / we should add this to the list of things to exactly specify.

@erictraut
Copy link
Collaborator

but treat it as an abstract class and error when you attempt to instantiate

Yeah, that makes sense. After reviewing the text in the Explicitly Declaring Implementation section, I agree that pyright should emit those errors only if it's marked @final. I'll make that change.

@hauntsaninja
Copy link
Collaborator Author

Okay, this is mergeable. Remaining items:

@hauntsaninja hauntsaninja merged commit 3c02f03 into python:main Jan 16, 2024
@hauntsaninja hauntsaninja deleted the generic-basic branch January 16, 2024 05:30
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.

2 participants