@@ -67,7 +67,7 @@ def get_annotation_module(annotation: Any) -> str:
67
67
if (
68
68
is_new_type
69
69
or isinstance (annotation , TypeVar )
70
- or type (annotation ).__name__ in ( "ParamSpec" , "ParamSpecArgs" , "ParamSpecKwargs" )
70
+ or type (annotation ).__name__ in { "ParamSpec" , "ParamSpecArgs" , "ParamSpecKwargs" }
71
71
):
72
72
return "typing"
73
73
if hasattr (annotation , "__module__" ):
@@ -107,7 +107,7 @@ def get_annotation_class_name(annotation: Any, module: str) -> str: # noqa: C90
107
107
return annotation .__qualname__ # type: ignore[no-any-return]
108
108
if getattr (annotation , "_name" , None ): # Required for generic aliases on Python 3.7+
109
109
return annotation ._name # type: ignore[no-any-return] # noqa: SLF001
110
- if module in ( "typing" , "typing_extensions" ) and isinstance (getattr (annotation , "name" , None ), str ):
110
+ if module in { "typing" , "typing_extensions" } and isinstance (getattr (annotation , "name" , None ), str ):
111
111
# Required for at least Pattern and Match
112
112
return annotation .name # type: ignore[no-any-return]
113
113
@@ -140,7 +140,7 @@ def get_annotation_args(annotation: Any, module: str, class_name: str) -> tuple[
140
140
return () # This is the original, not parametrized type
141
141
142
142
# Special cases
143
- if class_name in ( "Pattern" , "Match" ) and hasattr (annotation , "type_var" ): # Python < 3.7
143
+ if class_name in { "Pattern" , "Match" } and hasattr (annotation , "type_var" ): # Python < 3.7
144
144
return (annotation .type_var ,)
145
145
if class_name == "ClassVar" and hasattr (annotation , "__type__" ): # ClassVar on Python < 3.7
146
146
return (annotation .__type__ ,)
@@ -169,7 +169,7 @@ def format_internal_tuple(t: tuple[Any, ...], config: Config) -> str:
169
169
return f"({ ', ' .join (fmt )} )"
170
170
171
171
172
- def format_annotation (annotation : Any , config : Config ) -> str : # noqa: C901, PLR0911, PLR0912, PLR0915
172
+ def format_annotation (annotation : Any , config : Config ) -> str : # noqa: C901, PLR0911, PLR0912, PLR0915, PLR0914
173
173
"""
174
174
Format the annotation.
175
175
@@ -238,7 +238,7 @@ def format_annotation(annotation: Any, config: Config) -> str: # noqa: C901, PL
238
238
formatted_args = None if args else args_format
239
239
elif full_name == "typing.Optional" :
240
240
args = tuple (x for x in args if x is not type (None ))
241
- elif full_name in ( "typing.Union" , "types.UnionType" ) and type (None ) in args :
241
+ elif full_name in { "typing.Union" , "types.UnionType" } and type (None ) in args :
242
242
if len (args ) == 2 : # noqa: PLR2004
243
243
full_name = "typing.Optional"
244
244
role = "data"
@@ -250,7 +250,7 @@ def format_annotation(annotation: Any, config: Config) -> str: # noqa: C901, PL
250
250
role = "data"
251
251
args_format = f"\\ [:py:data:`{ prefix } typing.Union`\\ [{{}}]]"
252
252
args = tuple (x for x in args if x is not type (None ))
253
- elif full_name in ( "typing.Callable" , "collections.abc.Callable" ) and args and args [0 ] is not ...:
253
+ elif full_name in { "typing.Callable" , "collections.abc.Callable" } and args and args [0 ] is not ...:
254
254
fmt = [format_annotation (arg , config ) for arg in args ]
255
255
formatted_args = f"\\ [\\ [{ ', ' .join (fmt [:- 1 ])} ], { fmt [- 1 ]} ]"
256
256
elif full_name == "typing.Literal" :
@@ -316,7 +316,7 @@ def remove_prefix(text: str, prefix: str) -> str:
316
316
return "\n " .join (aligned_prefix + aligned_suffix )
317
317
318
318
319
- def process_signature ( # noqa: C901, PLR0913
319
+ def process_signature ( # noqa: C901, PLR0913, PLR0917
320
320
app : Sphinx ,
321
321
what : str ,
322
322
name : str ,
@@ -541,7 +541,7 @@ def _one_child(module: Module) -> stmt | None:
541
541
comment_args = split_type_comment_args (comment_args_str )
542
542
is_inline = len (comment_args ) == 1 and comment_args [0 ] == "..."
543
543
if not is_inline :
544
- if args and args [0 ].arg in ( "self" , "cls" ) and len (comment_args ) != len (args ):
544
+ if args and args [0 ].arg in { "self" , "cls" } and len (comment_args ) != len (args ):
545
545
comment_args .insert (0 , None ) # self/cls may be omitted in type comments, insert blank
546
546
547
547
if len (args ) != len (comment_args ):
@@ -590,9 +590,9 @@ def add(val: str) -> None:
590
590
591
591
brackets , start_arg_at , at = 0 , 0 , 0
592
592
for at , char in enumerate (comment ):
593
- if char in ( "[" , "(" ) :
593
+ if char in { "[" , "(" } :
594
594
brackets += 1
595
- elif char in ( "]" , ")" ) :
595
+ elif char in { "]" , ")" } :
596
596
brackets -= 1
597
597
elif char == "," and brackets == 0 :
598
598
add (comment [start_arg_at :at ])
@@ -616,7 +616,7 @@ def format_default(app: Sphinx, default: Any, is_annotated: bool) -> str | None:
616
616
return f"default: ``{ formatted } ``"
617
617
618
618
619
- def process_docstring ( # noqa: PLR0913
619
+ def process_docstring ( # noqa: PLR0913, PLR0917, PLR0917
620
620
app : Sphinx ,
621
621
what : str ,
622
622
name : str ,
@@ -695,7 +695,7 @@ def _line_is_param_line_for_arg(line: str, arg_name: str) -> bool:
695
695
return any (doc_name == prefix + arg_name for prefix in ("" , "\\ *" , "\\ **" , "\\ *\\ *" ))
696
696
697
697
698
- def _inject_types_to_docstring ( # noqa: PLR0913
698
+ def _inject_types_to_docstring ( # noqa: PLR0913, PLR0917
699
699
type_hints : dict [str , Any ],
700
700
signature : inspect .Signature | None ,
701
701
original_obj : Any ,
@@ -826,7 +826,7 @@ def get_insert_index(app: Sphinx, lines: list[str]) -> InsertIndexInfo | None:
826
826
827
827
# 4. Insert before examples
828
828
for child in doc .children :
829
- if tag_name (child ) in [ "literal_block" , "paragraph" , "field_list" ] :
829
+ if tag_name (child ) in { "literal_block" , "paragraph" , "field_list" } :
830
830
continue
831
831
line_no = node_line_no (child )
832
832
at = line_no - 2 if line_no else len (lines )
@@ -836,7 +836,7 @@ def get_insert_index(app: Sphinx, lines: list[str]) -> InsertIndexInfo | None:
836
836
return InsertIndexInfo (insert_index = len (lines ))
837
837
838
838
839
- def _inject_rtype ( # noqa: PLR0913
839
+ def _inject_rtype ( # noqa: PLR0913, PLR0917
840
840
type_hints : dict [str , Any ],
841
841
original_obj : Any ,
842
842
app : Sphinx ,
@@ -950,12 +950,12 @@ def setup(app: Sphinx) -> dict[str, bool]:
950
950
951
951
__all__ = [
952
952
"__version__" ,
953
+ "backfill_type_hints" ,
953
954
"format_annotation" ,
954
955
"get_annotation_args" ,
955
956
"get_annotation_class_name" ,
956
957
"get_annotation_module" ,
957
958
"normalize_source_lines" ,
958
959
"process_docstring" ,
959
960
"process_signature" ,
960
- "backfill_type_hints" ,
961
961
]
0 commit comments