Skip to content

Attempt to statically collect types in typeshed (ahead of time) #56

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

lagru
Copy link
Member

@lagru lagru commented May 27, 2025

A first shot at #42.

This is an attempt to build a map of all usable type imports in Python's _typeshed. That would make docstub a lot more able to match doctypes to a valid Python type.

I initially thought, I had gotten pretty far by re-purposing the TypeCollector class. Unfortunately, _typeshed makes heavy use of re-importing types. E.g. the content of collections.abc:

from _collections_abc import *
from _collections_abc import __all__ as __all__

This means the current approach in tools/collect_stdlib_types.py will miss a lot of types.

This means resolving references and semantic analysis which is a lot more tricky to do. I think it's possible but would take a lot of work that's not actually the focus of docstub. It would also re-implement the wheel since mypy, basedpyright, etc. must have already figured that out.

So from that point, I looked into mypy's internals (including stubtest & stubgen), aiming to leverage mypy to create the list of types in src/docstub/_stdlib_types.py. To be honest, I couldn't make a lot of sense of it, even armed with a debugger. 🙈

Besides asking for support on the appropriate mypy channel, I don't have a lot of concrete ideas on proceeding right now. I could explore using a proper language server. But that seems similarly complicated right now.

I could use intersphinx mappings like suggested in #22. But I'm not sure if that actually contains all types. It's an interesting approach. Not all third-party packages may provide an intersphinx mapping but there might be packages that do but don't have stubs available. And it's already pretty close to what I actually want as output...

@lagru lagru added enhancement New feature or request unclear More information / investigation needed labels May 27, 2025
@lagru lagru linked an issue May 27, 2025 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request unclear More information / investigation needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make it easy to use types from _typeshed in docstrings
1 participant