Additional type hinting - #2114
Conversation
|
As you noted there is a lot in this PR which will take some time to review. Some general comments based on the description:
|
|
Okay, I have reworked the way that the compression interface behaves:
One thing that bugs me with this change is that the |
IMO conceptually I think making it abstract and enforced is better, but I also don't know what existing implementations there are. I'd make it a major release just in case since it is a breaking change of something public |
There was a problem hiding this comment.
I think the protocol-vs-abstract-implementation issue is important, but in order to not block the rest of this PR I think it's fine to make it a protocol now and then maybe enforce it with abstract later (if that would indeed be a breaking change we can maybe put it in an ASDF 6.0 milestone)
|
@zacharyburnett agreed I think just changing it in the next major release makes the most sense. It's a breaking change but for it to actually be an issue someone would have to have implemented a custom compressor, inherited from |
|
would regression tests against |
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.Compressorfrom an abstract class to a runtime-checkable protocol.Compressor: previously a plugin only needed to implement one ofcompressordecompressbut now plugins are required to implement both.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