Add typing stubs#195
Open
StDymphna wants to merge 2 commits into
Open
Conversation
_frozen_importlib/_frozen_importlib_external
|
Hello, what do you think about contributing these stubs to typeshed? It seems like a good place to keep them until they’re included in the source code. Are you still interested in pursuing this idea? |
webknjaz
added a commit
to webknjaz/ansible
that referenced
this pull request
Nov 28, 2025
The stubs are taken from the unmerged PR [[1]] upstream. MyPy is set up to rely on them instead of the missing package type annotations. [1]: pypa/distlib#195
webknjaz
added a commit
to ansible/ansible
that referenced
this pull request
Dec 10, 2025
The stubs are taken from the unmerged PR [[1]] upstream. MyPy is set up to rely on them instead of the missing package type annotations. [1]: pypa/distlib#195
Member
|
Hey @vsajip, would you mind getting this patch in if @StDymphna rebases with conflict resolution? It seems good. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request adds some basic configuration for non-automated running of mypy, and obviously the typing stubs and py.typed marker. Only one source file was modified to replace the private
_frozen_importlib(_external)import withimportlib.machinery. I could see no reason for this being used, and it made typehinting theLoaders extremely verbose, difficult, and required silencing of multiple errors.If you do not want the maintenance-burden, feel free to close this, and I will submit to typeshed instead.
The stubs were compared to API reference docs. The private code that had doc-strings that were not exported in the API reference was also checked against, but there may be some discrepancies that I missed there.
In the stubs, I added simple
# documentedcomments to functions/classes/methods/attributes. Additionally, discrepancies like contradicting documentation such as missing or extra parameters, different parameter types, or normal attributes being called properties were commented on. You can get the more relevant comments with something likegrep -RIP '#(?!\s(?:documented$|START|END|noqa|@))'.However, in
distlib.resources, two probable Liskov Substitution Principle violations and one outright error are explicitly marked withFIXME. Besides these, it passes mypy's strict mode as well as stubtest.These stubs are essentially complete except for the omission of compat; however, for any maintainers or for anyone else that comes across this, the following are ways these stubs may be improved or revised:
Fix the commented documentation discrepancies and then remove said comments
Add
ClassVar[...]to class variables that should not be useable as an instance variable.Add
Final[...]to variables/attributes that should not be reassigned, redefined, or overridden. This alone is not quite the same as a constant. It does not work with try/except or with if/else statements unless it is written as a ternary.Finalonly prevents name re-binding. If immutability is desired, anABCis required (also see 6. below). Example from docs:4.1 Add
@finaldecorator to:A. Classes that should not be inherited from.
B. Methods that should not be overriden in a subclass.
4.2 Evaluate whether existing
@propertymethods should be@finalto prevent overriding.Add
TypedDicts fordistlib.metadata. In that module, I made a comment about this and not knowing all possible keys nor their acceptable types.Use
collection.abc ABC's for parameters where appropriate.Protocols could be used to enforce instance attribute requirements, e.g.distlib.index'sPackageIndex.upload_file'smetadataparameter is documented as requiring aMetadatainstance with at leastNameandVersionfields set. If this level of enforcement is desired, I believeProtocols can do it.