-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
stubtest: adjust symtable logic #16823
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
Fixes python/typeshed#11318