Skip to content

[PEP 695] Add more error checks and tests for error conditions #17339

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

Merged
merged 5 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mypy/errorcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ def __hash__(self) -> int:
del error_codes[FILE.code]

# This is a catch-all for remaining uncategorized errors.
MISC: Final = ErrorCode("misc", "Miscellaneous other checks", "General")
MISC: Final[ErrorCode] = ErrorCode("misc", "Miscellaneous other checks", "General")
Copy link
Member

Choose a reason for hiding this comment

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

Is this because we are not deferring top-levels, LOL?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Possibly, though I didn't look into this in any detail. This is not the first instance of this issue in this module.


OVERLOAD_OVERLAP: Final[ErrorCode] = ErrorCode(
"overload-overlap",
Expand Down
12 changes: 10 additions & 2 deletions mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -1185,8 +1185,16 @@ def translate_type_params(self, type_params: list[Any]) -> list[TypeParam]:
explicit_type_params.append(TypeParam(p.name, TYPE_VAR_TUPLE_KIND, None, []))
else:
if isinstance(p.bound, ast3.Tuple):
conv = TypeConverter(self.errors, line=p.lineno)
values = [conv.visit(t) for t in p.bound.elts]
if len(p.bound.elts) < 2:
self.fail(
message_registry.TYPE_VAR_TOO_FEW_CONSTRAINED_TYPES,
p.lineno,
p.col_offset,
blocker=False,
)
else:
conv = TypeConverter(self.errors, line=p.lineno)
values = [conv.visit(t) for t in p.bound.elts]
elif p.bound is not None:
bound = TypeConverter(self.errors, line=p.lineno).visit(p.bound)
explicit_type_params.append(TypeParam(p.name, TYPE_VAR_KIND, bound, values))
Expand Down
3 changes: 3 additions & 0 deletions mypy/message_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,3 +330,6 @@ def with_additional_msg(self, info: str) -> ErrorMessage:
NARROWED_TYPE_NOT_SUBTYPE: Final = ErrorMessage(
"Narrowed type {} is not a subtype of input type {}", codes.NARROWED_TYPE_NOT_SUBTYPE
)
TYPE_VAR_TOO_FEW_CONSTRAINED_TYPES: Final = ErrorMessage(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can this message be reused in semanal.py where a TypeVar cannot have only a single constraint message is used inline? This should help with discoverability of two TypeVar syntax variants constraints, because they share some common argument validation logic.

Copy link
Member

Choose a reason for hiding this comment

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

I agree, if there are not too many tests to update, we should reuse the same message for both old and new style variables.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I made the error messages consistent.

"Type variable must have at least two constrained types", codes.MISC
)
31 changes: 31 additions & 0 deletions test-data/unit/check-python312.test
Original file line number Diff line number Diff line change
Expand Up @@ -1494,3 +1494,34 @@ reveal_type(a) # N: Revealed type is "builtins.list[builtins.int]"
# flags: --enable-incomplete-feature=NewGenericSyntax
def f[T](x: foobar, y: T) -> T: ... # E: Name "foobar" is not defined
reveal_type(f) # N: Revealed type is "def [T] (x: Any, y: T`-1) -> T`-1"

[case testPEP695WrongNumberOfConstrainedTypes]
# flags: --enable-incomplete-feature=NewGenericSyntax
type A[T: ()] = list[T] # E: Type variable must have at least two constrained types
a: A[int]
reveal_type(a) # N: Revealed type is "builtins.list[builtins.int]"

type B[T: (int,)] = list[T] # E: Type variable must have at least two constrained types
b: B[str]
reveal_type(b) # N: Revealed type is "builtins.list[builtins.str]"

[case testPEP695UsingTypeVariableInOwnBoundOrConstraint]
# flags: --enable-incomplete-feature=NewGenericSyntax
type A[T: list[T]] = str # E: Name "T" is not defined
type B[S: (list[S], str)] = str # E: Name "S" is not defined
type C[T, S: list[T]] = str # E: Name "T" is not defined

def f[T: T](x: T) -> T: ... # E: Name "T" is not defined
class D[T: T]: # E: Name "T" is not defined
pass

[case testPEP695InvalidType]
# flags: --enable-incomplete-feature=NewGenericSyntax
def f[T: 1](x: T) -> T: ... # E: Invalid type: try using Literal[1] instead?
class C[T: (int, (1 + 2))]: pass # E: Invalid type comment or annotation
type A = list[1] # E: Invalid type: try using Literal[1] instead?
type B = (1 + 2) # E: Invalid type alias: expression is not a valid type
a: A
reveal_type(a) # N: Revealed type is "builtins.list[Any]"
b: B
reveal_type(b) # N: Revealed type is "Any"
Loading