-
Notifications
You must be signed in to change notification settings - Fork 0
Support type aliases, NamedTuple
and TypedDict
in constrained TypeVar defaults
#2
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
base: master
Are you sure you want to change the base?
Changes from all commits
1fed961
c7b9670
a871b87
57799f1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -2658,7 +2658,7 @@ def check_typevar_defaults(self, tvars: Sequence[TypeVarLikeType]) -> None: | |||||||
continue | ||||||||
if not is_subtype(tv.default, tv.upper_bound): | ||||||||
self.fail("TypeVar default must be a subtype of the bound type", tv) | ||||||||
if tv.values and not any(tv.default == value for value in tv.values): | ||||||||
if tv.values and not any(is_same_type(tv.default, value) for value in tv.values): | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Type Comparison LogicThe change from equality check to is_same_type() affects type comparison behavior. While this fixes the immediate issue, it may have unintended consequences for other type comparisons that relied on exact equality. This could lead to incorrect type validation in other scenarios.
Commitable Suggestion
Suggested change
Standards
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Duplicate Equality CheckThe equality check was changed from '==' to 'is_same_type()' function. This same change was also made in checkexpr.py, suggesting a common operation that could be extracted to avoid duplication and ensure consistent behavior across the codebase. Standards
|
||||||||
self.fail("TypeVar default must be one of the constraint types", tv) | ||||||||
|
||||||||
def check_enum(self, defn: ClassDef) -> None: | ||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -6210,7 +6210,7 @@ def visit_type_var_expr(self, e: TypeVarExpr) -> Type: | |||||||||||
): | ||||||||||||
if not is_subtype(p_default, e.upper_bound): | ||||||||||||
self.chk.fail("TypeVar default must be a subtype of the bound type", e) | ||||||||||||
if e.values and not any(p_default == value for value in e.values): | ||||||||||||
if e.values and not any(is_same_type(p_default, value) for value in e.values): | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inconsistent Type ComparisonUsing equality (==) instead of is_same_type() for type comparison in TypeVar expression validation. This creates inconsistent behavior between checker.py and checkexpr.py, potentially causing different validation results for the same types.
Commitable Suggestion
Suggested change
Standards
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inconsistent Type ComparisonThe same type comparison logic change was made in checkexpr.py, creating a parallel modification. If these functions are used in different contexts, inconsistent behavior could occur if one is updated without the other in future changes, leading to reliability issues.
Commitable Suggestion
Suggested change
Standards
|
||||||||||||
self.chk.fail("TypeVar default must be one of the constraint types", e) | ||||||||||||
return AnyType(TypeOfAny.special_form) | ||||||||||||
|
||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Type Comparison Issue
Using equality (==) instead of is_same_type() for type comparison can lead to incorrect constraint validation. This may cause TypeVar defaults that are semantically equivalent but not identical to be incorrectly rejected, reducing type system reliability.
Commitable Suggestion
Standards