Skip to content
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

Add conformance tests for basic generic spec #1553

Merged
merged 13 commits into from
Jan 16, 2024

Conversation

hauntsaninja
Copy link
Collaborator

No description provided.

@hauntsaninja hauntsaninja marked this pull request as ready for review January 12, 2024 05:53
docs/spec/generics.rst Show resolved Hide resolved
conformance/tests/generics_basic.py Outdated Show resolved Hide resolved
conformance/tests/generics_basic.py Outdated Show resolved Hide resolved
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.

conformance/tests/generics_basic.py Show resolved Hide resolved
conformance/tests/generics_scoping.py Outdated Show resolved Hide resolved
conformance/tests/generics_scoping.py Show resolved Hide resolved
conformance/tests/generics_scoping.py Show resolved Hide resolved
conformance/tests/generics_scoping.py Show resolved Hide resolved
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
4 checks passed
@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