flake8-pyi: fix PYI015 false positive on assignment of TypeVar & friends (#3861)

This commit is contained in:
Ran Benita 2023-04-03 18:28:46 +03:00 committed by GitHub
parent 25771cd4b9
commit d2f2544f6e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 69 additions and 45 deletions

View file

@ -1,6 +1,6 @@
import builtins import builtins
import typing import typing
from typing import TypeAlias, Final from typing import TypeAlias, Final, NewType, TypeVar, TypeVarTuple, ParamSpec
# We shouldn't emit Y015 for simple default values # We shouldn't emit Y015 for simple default values
field1: int field1: int
@ -26,6 +26,10 @@ field9 = None # Y026 Use typing_extensions.TypeAlias for type aliases, e.g. "fi
Field95: TypeAlias = None Field95: TypeAlias = None
Field96: TypeAlias = int | None Field96: TypeAlias = int | None
Field97: TypeAlias = None | typing.SupportsInt | builtins.str | float | bool Field97: TypeAlias = None | typing.SupportsInt | builtins.str | float | bool
Field98 = NewType('MyInt', int)
Field99 = TypeVar('Field99')
Field100 = TypeVarTuple('Field100')
Field101 = ParamSpec('Field101')
field19 = [1, 2, 3] # Y052 Need type annotation for "field19" field19 = [1, 2, 3] # Y052 Need type annotation for "field19"
field191: list[int] = [1, 2, 3] field191: list[int] = [1, 2, 3]
field20 = (1, 2, 3) # Y052 Need type annotation for "field20" field20 = (1, 2, 3) # Y052 Need type annotation for "field20"

View file

@ -221,6 +221,23 @@ fn is_valid_default_value_with_annotation(
false false
} }
pub fn is_type_var_call(checker: &Checker, expr: &Expr) -> bool {
let ExprKind::Call {func, ..} = &expr.node else {
return false;
};
checker
.ctx
.resolve_call_path(func)
.map_or(false, |call_path| {
call_path.as_slice() == ["typing", "TypeVar"]
|| call_path.as_slice() == ["typing", "TypeVarTuple"]
|| call_path.as_slice() == ["typing_extensions", "TypeVarTuple"]
|| call_path.as_slice() == ["typing", "NewType"]
|| call_path.as_slice() == ["typing", "ParamSpec"]
|| call_path.as_slice() == ["typing_extensions", "ParamSpec"]
})
}
/// PYI011 /// PYI011
pub fn typed_argument_simple_defaults(checker: &mut Checker, args: &Arguments) { pub fn typed_argument_simple_defaults(checker: &mut Checker, args: &Arguments) {
if !args.defaults.is_empty() { if !args.defaults.is_empty() {
@ -342,6 +359,9 @@ pub fn assignment_default_in_stub(checker: &mut Checker, value: &Expr, annotatio
}) { }) {
return; return;
} }
if is_type_var_call(checker, value) {
return;
}
if !is_valid_default_value_with_annotation(value, checker, true) { if !is_valid_default_value_with_annotation(value, checker, true) {
let mut diagnostic = Diagnostic::new(AssignmentDefaultInStub, Range::from(value)); let mut diagnostic = Diagnostic::new(AssignmentDefaultInStub, Range::from(value));

View file

@ -8,19 +8,19 @@ expression: diagnostics
suggestion: "Replace default value with `...`" suggestion: "Replace default value with `...`"
fixable: true fixable: true
location: location:
row: 40 row: 44
column: 22 column: 22
end_location: end_location:
row: 40 row: 44
column: 57 column: 57
fix: fix:
edits: edits:
- content: "..." - content: "..."
location: location:
row: 40 row: 44
column: 22 column: 22
end_location: end_location:
row: 40 row: 44
column: 57 column: 57
parent: ~ parent: ~
- kind: - kind:
@ -29,19 +29,19 @@ expression: diagnostics
suggestion: "Replace default value with `...`" suggestion: "Replace default value with `...`"
fixable: true fixable: true
location: location:
row: 41 row: 45
column: 22 column: 22
end_location: end_location:
row: 41 row: 45
column: 34 column: 34
fix: fix:
edits: edits:
- content: "..." - content: "..."
location: location:
row: 41 row: 45
column: 22 column: 22
end_location: end_location:
row: 41 row: 45
column: 34 column: 34
parent: ~ parent: ~
- kind: - kind:
@ -50,19 +50,19 @@ expression: diagnostics
suggestion: "Replace default value with `...`" suggestion: "Replace default value with `...`"
fixable: true fixable: true
location: location:
row: 42 row: 46
column: 22 column: 22
end_location: end_location:
row: 42 row: 46
column: 37 column: 37
fix: fix:
edits: edits:
- content: "..." - content: "..."
location: location:
row: 42 row: 46
column: 22 column: 22
end_location: end_location:
row: 42 row: 46
column: 37 column: 37
parent: ~ parent: ~
- kind: - kind:
@ -71,19 +71,19 @@ expression: diagnostics
suggestion: "Replace default value with `...`" suggestion: "Replace default value with `...`"
fixable: true fixable: true
location: location:
row: 43 row: 47
column: 25 column: 25
end_location: end_location:
row: 43 row: 47
column: 35 column: 35
fix: fix:
edits: edits:
- content: "..." - content: "..."
location: location:
row: 43 row: 47
column: 25 column: 25
end_location: end_location:
row: 43 row: 47
column: 35 column: 35
parent: ~ parent: ~
- kind: - kind:
@ -92,19 +92,19 @@ expression: diagnostics
suggestion: "Replace default value with `...`" suggestion: "Replace default value with `...`"
fixable: true fixable: true
location: location:
row: 44 row: 48
column: 46 column: 46
end_location: end_location:
row: 44 row: 48
column: 69 column: 69
fix: fix:
edits: edits:
- content: "..." - content: "..."
location: location:
row: 44 row: 48
column: 46 column: 46
end_location: end_location:
row: 44 row: 48
column: 69 column: 69
parent: ~ parent: ~
- kind: - kind:
@ -113,19 +113,19 @@ expression: diagnostics
suggestion: "Replace default value with `...`" suggestion: "Replace default value with `...`"
fixable: true fixable: true
location: location:
row: 45 row: 49
column: 30 column: 30
end_location: end_location:
row: 45 row: 49
column: 53 column: 53
fix: fix:
edits: edits:
- content: "..." - content: "..."
location: location:
row: 45 row: 49
column: 30 column: 30
end_location: end_location:
row: 45 row: 49
column: 53 column: 53
parent: ~ parent: ~
- kind: - kind:
@ -134,19 +134,19 @@ expression: diagnostics
suggestion: "Replace default value with `...`" suggestion: "Replace default value with `...`"
fixable: true fixable: true
location: location:
row: 46 row: 50
column: 36 column: 36
end_location: end_location:
row: 46 row: 50
column: 47 column: 47
fix: fix:
edits: edits:
- content: "..." - content: "..."
location: location:
row: 46 row: 50
column: 36 column: 36
end_location: end_location:
row: 46 row: 50
column: 47 column: 47
parent: ~ parent: ~
- kind: - kind:
@ -155,19 +155,19 @@ expression: diagnostics
suggestion: "Replace default value with `...`" suggestion: "Replace default value with `...`"
fixable: true fixable: true
location: location:
row: 48 row: 52
column: 27 column: 27
end_location: end_location:
row: 48 row: 52
column: 43 column: 43
fix: fix:
edits: edits:
- content: "..." - content: "..."
location: location:
row: 48 row: 52
column: 27 column: 27
end_location: end_location:
row: 48 row: 52
column: 43 column: 43
parent: ~ parent: ~
- kind: - kind:
@ -176,19 +176,19 @@ expression: diagnostics
suggestion: "Replace default value with `...`" suggestion: "Replace default value with `...`"
fixable: true fixable: true
location: location:
row: 49 row: 53
column: 10 column: 10
end_location: end_location:
row: 49 row: 53
column: 23 column: 23
fix: fix:
edits: edits:
- content: "..." - content: "..."
location: location:
row: 49 row: 53
column: 10 column: 10
end_location: end_location:
row: 49 row: 53
column: 23 column: 23
parent: ~ parent: ~
- kind: - kind:
@ -197,19 +197,19 @@ expression: diagnostics
suggestion: "Replace default value with `...`" suggestion: "Replace default value with `...`"
fixable: true fixable: true
location: location:
row: 50 row: 54
column: 10 column: 10
end_location: end_location:
row: 50 row: 54
column: 25 column: 25
fix: fix:
edits: edits:
- content: "..." - content: "..."
location: location:
row: 50 row: 54
column: 10 column: 10
end_location: end_location:
row: 50 row: 54
column: 25 column: 25
parent: ~ parent: ~
- kind: - kind:
@ -218,19 +218,19 @@ expression: diagnostics
suggestion: "Replace default value with `...`" suggestion: "Replace default value with `...`"
fixable: true fixable: true
location: location:
row: 51 row: 55
column: 10 column: 10
end_location: end_location:
row: 51 row: 55
column: 15 column: 15
fix: fix:
edits: edits:
- content: "..." - content: "..."
location: location:
row: 51 row: 55
column: 10 column: 10
end_location: end_location:
row: 51 row: 55
column: 15 column: 15
parent: ~ parent: ~