From 13c626a9e70401eaf6c787cac522828f61460585 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Wed, 11 Jun 2025 11:53:02 -0400 Subject: [PATCH] fix false positive with TypeVar defaults with more than one argument --- pycodestyle.py | 15 ++++++++------- testing/data/python313.py | 4 ++++ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/pycodestyle.py b/pycodestyle.py index 96291b0d..3439f35a 100755 --- a/pycodestyle.py +++ b/pycodestyle.py @@ -1066,12 +1066,6 @@ def whitespace_around_named_parameter_equals(logical_line, tokens): if token_type == tokenize.OP: if text in '([': paren_stack.append(text) - # PEP 696 defaults always use spaced-style `=` - # type A[T = default] = ... - # def f[T = default](): ... - # class C[T = default](): ... - if in_generic and paren_stack == ['[']: - annotated_func_arg = True elif text in ')]' and paren_stack: paren_stack.pop() # def f(arg: tp = default): ... @@ -1080,7 +1074,14 @@ def whitespace_around_named_parameter_equals(logical_line, tokens): elif len(paren_stack) == 1 and text == ',': annotated_func_arg = False elif paren_stack and text == '=': - if annotated_func_arg and len(paren_stack) == 1: + if ( + # PEP 696 defaults always use spaced-style `=` + # type A[T = default] = ... + # def f[T = default](): ... + # class C[T = default](): ... + (in_generic and paren_stack == ['[']) or + (annotated_func_arg and paren_stack == ['(']) + ): require_space = True if start == prev_end: yield (prev_end, missing_message) diff --git a/testing/data/python313.py b/testing/data/python313.py index 5540aced..ae70e427 100644 --- a/testing/data/python313.py +++ b/testing/data/python313.py @@ -10,6 +10,10 @@ class C2[T = str]: pass +class C3[T, U: str = str]: + pass + + def f[T: (int, str) = str](t: T) -> T: pass