Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fix self-referential upper bound in new-style type variables#17407

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
JelleZijlstra merged 2 commits intopython:masterfromilevkivskyi:fix-type-var-bound
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from1 commit
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
NextNext commit
Fix self-referential upper bound in new-style type variables
  • Loading branch information
@ilevkivskyi
ilevkivskyi committedJun 19, 2024
commitfa3bd32df5e2dcb625ba50ef73a34027aaf86aa9
1 change: 0 additions & 1 deletionmypy/plugin.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -328,7 +328,6 @@ def anal_type(
allow_tuple_literal: bool = False,
allow_unbound_tvars: bool = False,
report_invalid_types: bool = True,
third_pass: bool = False,
) -> Type | None:
"""Analyze an unbound type.

Expand Down
13 changes: 6 additions & 7 deletionsmypy/semanal.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1738,10 +1738,12 @@ 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)
upper_bound = self.anal_type(type_param.upper_bound, allow_placeholder=True)
# TODO: we should validate the upper bound is valid for a given kind.
if upper_bound is None:
return None
# This and below copies special-casing for old-style type variables, that
# is equally necessary for new-style classes to break a vicious circle.
upper_bound = PlaceholderType(None, [], context.line)
else:
if type_param.kind == TYPE_VAR_TUPLE_KIND:
upper_bound = self.named_type("builtins.tuple", [self.object_type()])
Expand All@@ -1752,9 +1754,9 @@ def analyze_type_param(
values = []
if type_param.values:
for value in type_param.values:
analyzed = self.anal_type(value)
analyzed = self.anal_type(value, allow_placeholder=True)
if analyzed is None:
returnNone
analyzed = PlaceholderType(None, [], context.line)
values.append(analyzed)
return TypeVarExpr(
name=type_param.name,
Expand DownExpand Up@@ -7192,16 +7194,13 @@ def anal_type(
report_invalid_types: bool = True,
prohibit_self_type: str | None = None,
allow_type_any: bool = False,
third_pass: bool = False,
) -> Type | None:
"""Semantically analyze a type.

Args:
typ: Type to analyze (if already analyzed, this is a no-op)
allow_placeholder: If True, may return PlaceholderType if
encountering an incomplete definition
third_pass: Unused; only for compatibility with old semantic
analyzer

Return None only if some part of the type couldn't be bound *and* it
referred to an incomplete namespace or definition. In this case also
Expand Down
13 changes: 13 additions & 0 deletionstest-data/unit/check-python312.test
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1591,3 +1591,16 @@ c: E[str]
d: E[int] # E: Type argument "int" of "E" must be a subtype of "str"
[builtins fixtures/tuple.pyi]
[typing fixtures/typing-full.pyi]

[case testCurrentClassWorksAsBound]
# flags: --enable-incomplete-feature=NewGenericSyntax
from typing import Protocol

class Comparable[T: Comparable](Protocol):
def compare(self, other: T) -> bool: ...

class Good:
def compare(self, other: Good) -> bool: ...

x: Comparable[Good]
y: Comparable[int] # E: Type argument "int" of "Comparable" must be a subtype of "Comparable[Any]"

[8]ページ先頭

©2009-2025 Movatter.jp