File tree Expand file tree Collapse file tree 2 files changed +36
-1
lines changed Expand file tree Collapse file tree 2 files changed +36
-1
lines changed Original file line number Diff line number Diff line change @@ -255,7 +255,7 @@ def visit_unbound_type(self, t: UnboundType) -> str:
255255 s = t .name
256256 self .stubgen .import_tracker .require_name (s )
257257 if t .args :
258- s += '[{}]' .format (self .list_str (t .args ))
258+ s += '[{}]' .format (self .args_str (t .args ))
259259 return s
260260
261261 def visit_none_type (self , t : NoneType ) -> str :
@@ -264,6 +264,22 @@ def visit_none_type(self, t: NoneType) -> str:
264264 def visit_type_list (self , t : TypeList ) -> str :
265265 return '[{}]' .format (self .list_str (t .items ))
266266
267+ def args_str (self , args : Iterable [Type ]) -> str :
268+ """Convert an array of arguments to strings and join the results with commas.
269+
270+ The main difference from list_str is the preservation of quotes for string
271+ arguments
272+ """
273+ types = ['builtins.bytes' , 'builtins.unicode' ]
274+ res = []
275+ for arg in args :
276+ arg_str = arg .accept (self )
277+ if isinstance (arg , UnboundType ) and arg .original_str_fallback in types :
278+ res .append ("'{}'" .format (arg_str ))
279+ else :
280+ res .append (arg_str )
281+ return ', ' .join (res )
282+
267283
268284class AliasPrinter (NodeVisitor [str ]):
269285 """Visitor used to collect type aliases _and_ type variable definitions.
Original file line number Diff line number Diff line change @@ -64,6 +64,25 @@ def g(x: Foo = Foo()) -> Bar: ...
6464def f(x: Foo) -> Bar: ...
6565def g(x: Foo = ...) -> Bar: ...
6666
67+ [case testPreserveFunctionAnnotationWithArgs]
68+ def f(x: foo['x']) -> bar: ...
69+ def g(x: foo[x]) -> bar: ...
70+ def h(x: foo['x', 'y']) -> bar: ...
71+ def i(x: foo[x, y]) -> bar: ...
72+ def j(x: foo['x', y]) -> bar: ...
73+ def k(x: foo[x, 'y']) -> bar: ...
74+ def lit_str(x: Literal['str']) -> Literal['str']: ...
75+ def lit_int(x: Literal[1]) -> Literal[1]: ...
76+ [out]
77+ def f(x: foo['x']) -> bar: ...
78+ def g(x: foo[x]) -> bar: ...
79+ def h(x: foo['x', 'y']) -> bar: ...
80+ def i(x: foo[x, y]) -> bar: ...
81+ def j(x: foo['x', y]) -> bar: ...
82+ def k(x: foo[x, 'y']) -> bar: ...
83+ def lit_str(x: Literal['str']) -> Literal['str']: ...
84+ def lit_int(x: Literal[1]) -> Literal[1]: ...
85+
6786[case testPreserveVarAnnotation]
6887x: Foo
6988[out]
You can’t perform that action at this time.
0 commit comments