stubtest: adjust symtable logic#16823
Conversation
AlexWaygood
left a comment
There was a problem hiding this comment.
Nice! Not 100% sure about relying on __module__ so much (see my comment below), but the tweak to the symtable logic is definitely an improvement
|
It looks like this PR causes stubtest to start emitting 5 new errors when run on typeshed's stdlib stubs: It looks like these are all functions and classes that originate from other modules but are aliased in these modules using assignments (meaning they pass the def _belongs_to_runtime(r: types.ModuleType, attr: str) -> bool:
"""Heuristics to determine whether a name originates from another module."""
obj = getattr(r, attr)
if isinstance(obj, types.ModuleType):
return False
obj_mod = getattr(obj, "__module__", None)
symbols = _module_symbol_table(r)
if symbols is not None:
# if symtable says we got this from another module, return False
for sym in symbols.get_symbols():
if sym.is_imported() and attr == sym.get_name():
return False
# for functions and classes, the `__module__` attribute is very reliable;
# try that next
if callable(obj) and isinstance(obj_mod, str):
return bool(obj_mod == r.__name__)
# if symtable says that we assigned this symbol in the module,
# return True
for sym in symbols.get_symbols():
if sym.is_assigned() and attr == sym.get_name():
return True
# The __module__ attribute is pretty unreliable
# for anything except functions and classes,
# but it's maybe the best clue we've got at this point
if isinstance(obj_mod, str):
return bool(obj_mod == r.__name__)
return True |
|
Hm, thanks for running that and apologies that I didn't! However, I think I'm at a different conclusion... |
AlexWaygood
left a comment
There was a problem hiding this comment.
Good points -- I didn't look at those new errors closely enough. I'm persuaded, thanks! A couple optional nits below, but this LGTM:
|
I realised we can just lookup the symtable directly :-) |
|
Thanks for the reviews! (and also for inventing this symtable approach in the first place) |
Fixes python/typeshed#11318