Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
12 changes: 10 additions & 2 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1888,7 +1888,11 @@ def analyze_type_param(
) -> TypeVarLikeExpr | None:
fullname = self.qualified_name(type_param.name)
if type_param.upper_bound:
upper_bound = self.anal_type(type_param.upper_bound, allow_placeholder=True)
upper_bound = self.anal_type(
type_param.upper_bound,
allow_placeholder=True,
prohibit_self_type="a type parameter bound",
)
# TODO: we should validate the upper bound is valid for a given kind.
if upper_bound is None:
# This and below copies special-casing for old-style type variables, that
Expand Down Expand Up @@ -1929,7 +1933,11 @@ def analyze_type_param(
values: list[Type] = []
if type_param.values:
for value in type_param.values:
analyzed = self.anal_type(value, allow_placeholder=True)
analyzed = self.anal_type(
value,
allow_placeholder=True,
prohibit_self_type="a type parameter constraint",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think mypy already rejects Self in constraints. Why is this needed, would you elaborate?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think mypy already rejects Self in constraints. Why is this needed, would you elaborate?

class Foo:
    # TypeVar constraint type cannot be parametrized by type variables
    def constrained[T: (Foo, Self)](self: T) -> None: pass

)
if analyzed is None:
analyzed = PlaceholderType(None, [], context.line)
if has_type_vars(analyzed):
Expand Down
8 changes: 8 additions & 0 deletions test-data/unit/check-python312.test
Original file line number Diff line number Diff line change
Expand Up @@ -1446,6 +1446,14 @@ reveal_type(F[str]().m()) # N: Revealed type is "__main__.F[builtins.str]"
reveal_type(F[str]().mm(b'x')) # N: Revealed type is "tuple[__main__.F[builtins.str], builtins.bytes]"
[builtins fixtures/tuple.pyi]

[case testTypingSelfInvalidLocationsTypeParams]
# flags: --python-version 3.12
from typing import Self

class C:
def bounded[T: Self](self: T) -> None: ... # E: Self type cannot be used in a type parameter bound
def constrained[T: (C, Self)](self: T) -> None: ... # E: Self type cannot be used in a type parameter constraint

[case testPEP695CallAlias]
class C:
def __init__(self, x: str) -> None: ...
Expand Down
Loading