Skip to content

Commit

Permalink
stubgen: Use _typeshed.Incomplete instead of typing.Any (#12449)
Browse files Browse the repository at this point in the history
Since python/typeshed#7535, typeshed has a new type `_typeshed.Incomplete`,
intended to mark fields that are partly annotated, but need more manual
investigation. This applies to all types generated by stubgen, except
when explicitly annotated with `Any` in the implementation.
  • Loading branch information
srittau authored Mar 25, 2022
1 parent 6b0e848 commit 82bc8df
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 89 deletions.
29 changes: 15 additions & 14 deletions mypy/stubgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,7 @@ def visit_mypy_file(self, o: MypyFile) -> None:
self.defined_names = find_defined_names(o)
self.referenced_names = find_referenced_names(o)
known_imports = {
"_typeshed": ["Incomplete"],
"typing": ["Any", "TypeVar"],
"collections.abc": ["Generator"],
}
Expand Down Expand Up @@ -689,14 +690,14 @@ def visit_func_def(self, o: FuncDef, is_abstract: bool = False,
return_name = 'None'
for expr, in_assignment in all_yield_expressions(o):
if expr.expr is not None and not self.is_none_expr(expr.expr):
self.add_typing_import('Any')
yield_name = 'Any'
self.add_typing_import('Incomplete')
yield_name = 'Incomplete'
if in_assignment:
self.add_typing_import('Any')
send_name = 'Any'
self.add_typing_import('Incomplete')
send_name = 'Incomplete'
if has_return_statement(o):
self.add_typing_import('Any')
return_name = 'Any'
self.add_typing_import('Incomplete')
return_name = 'Incomplete'
generator_name = self.typing_name('Generator')
retname = f'{generator_name}[{yield_name}, {send_name}, {return_name}]'
elif not has_return_statement(o) and not is_abstract:
Expand Down Expand Up @@ -954,18 +955,18 @@ def process_namedtuple(self, lvalue: NameExpr, rvalue: CallExpr) -> None:
list_items = cast(List[StrExpr], rvalue.args[1].items)
items = [item.value for item in list_items]
else:
self.add('%s%s: Any' % (self._indent, lvalue.name))
self.import_tracker.require_name('Any')
self.add('%s%s: Incomplete' % (self._indent, lvalue.name))
self.import_tracker.require_name('Incomplete')
return
self.import_tracker.require_name('NamedTuple')
self.add('{}class {}(NamedTuple):'.format(self._indent, lvalue.name))
if len(items) == 0:
self.add(' ...\n')
else:
self.import_tracker.require_name('Any')
self.import_tracker.require_name('Incomplete')
self.add('\n')
for item in items:
self.add('{} {}: Any\n'.format(self._indent, item))
self.add('{} {}: Incomplete\n'.format(self._indent, item))
self._state = CLASS

def is_alias_expression(self, expr: Expression, top_level: bool = True) -> bool:
Expand Down Expand Up @@ -1220,11 +1221,11 @@ def get_str_type_of_node(self, rvalue: Expression,
return 'bool'
if can_infer_optional and \
isinstance(rvalue, NameExpr) and rvalue.name == 'None':
self.add_typing_import('Any')
return '{} | None'.format(self.typing_name('Any'))
self.add_typing_import('Incomplete')
return '{} | None'.format(self.typing_name('Incomplete'))
if can_be_any:
self.add_typing_import('Any')
return self.typing_name('Any')
self.add_typing_import('Incomplete')
return self.typing_name('Incomplete')
else:
return ''

Expand Down
Loading

0 comments on commit 82bc8df

Please sign in to comment.