Additional type hinting - #2114
Open
sydduckworth wants to merge 10 commits into
Open
Conversation
sydduckworth
marked this pull request as ready for review
August 12, 2026 17:17
Contributor
|
As you noted there is a lot in this PR which will take some time to review. Some general comments based on the description:
|
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.
Sorry about the size of this PR! Typing changes just kept cascading into each other 😭
Description
This PR adds type hints to a significant portion of the codebase. It also converts/replaces all of the abstract classes that use
__subclasshook__with protocols that work with type-checking.Compared to my previous type hint PR this PR required notably fewer functional code changes to avoid typing errors, which is probably a good sign.
Interface/Protocol Changes
Converterfrom an abstract class to a runtime-checkable protocol.isinstancethey don't supportissubclassif they have non-method members (which includes properties). I am assuming/hoping no external code is doingissubclass(Converter, ...). If this is a problem we can revertConverterand add a new protocol with a different name thatConverterinherits from (the reason to not just do this to start with is it adds more complexity and a disconnect between runtime and type-checking).Converter(andConverterProxy) are now generic over both object type and node type, with node type defaulting toYamlNode. For example, a converter that convertsMyClassinto arbitrary YAML could inheritConverter[MyClass]. A converter that convertsdatetimeinto a string could inheritConverter[datetime, str]. Surprisingly, Pyrefly seems to be able to infer the generic types for protocols decently well, even when dealing with duck-typed implementations.Compressorinto multiple protocols for type-checking, while keeping the existingCompressorfor backwards compatibility.Compressorrequires subclasses to implementcompressordecompressbut does not require both, which isn't something that can really be expressed in a single Python protocol.CompressorPlugincontains the corelabelproperty.Compresscontains thecompressmethod.Decompresscontains thedecompressmethod.Compressorinherits from bothCompressandDecompressso anything inheriting fromCompressorwill still work the same.Compressorin favor of the new protocols.ExtensionExtensionLikeprotocol fromasdf.typingtoasdf.extension. There hasn't been a release since it was added so this should be fineExtensionandExtensionProxyto inherit fromExtensionLike(ExtensionProxyshould not have been subclassingExtensionbecause it has methods with the same names but meaningfully different return types!)ExtensionLikewhere any extension object works andExtensionProxywhere the full set of methods are required.General Changes
asdf.extensionmodule and submodulesresource.py,versioning.py,_compression.py, and_node_info.py.test_extension.py)AI Disclosure
No AI tools used
Tasks
prekon your machinepyteston your machineno-changelog-entry-needed)changes/:echo "changed something" > changes/<PR#>.<changetype>.rst(see below for change types)docs/pagenews fragment change types...
changes/<PR#>.feature.rst: new featurechanges/<PR#>.bugfix.rst: bug fixchanges/<PR#>.doc.rst: documentation changechanges/<PR#>.removal.rst: deprecation or removal of public APIchanges/<PR#>.general.rst: infrastructure or miscellaneous change