Add a funsor.typing module for runtime dispatch and type-checking#451
Merged
Add a funsor.typing module for runtime dispatch and type-checking#451
Conversation
fritzo
approved these changes
Feb 21, 2021
Member
|
nit: could you add a funsor.typing section to the docs, and ensure docs are correctly generated? |
Member
Author
Docs now seem to render correctly for me locally. |
This was referenced Feb 22, 2021
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.
Addresses #351, #355. Supercedes #433, #434. Would unblock #423, although it might be easier to fix that independently.
Overview
This PR adds a self-contained
funsor.typingmodule for runtime dispatch and typechecking of parametric types, especially Funsor term types.It contains three distinct pieces of functionality extracted from
FunsorMeta:type,isinstanceandissubclassthat are compatible with a limited subset of Funsor/typing-style generic types, including Funsor terms,typing.Tuples andtyping.FrozenSets, mimicking similar APIs inpytypes(albeit with much less coverage oftyping).funsor.typing.GenericTypeMetawhose purpose is to support parametric subtyping, handle cons-hashing of parametric types and support runtimeissubclasschecks, and a minimal introspection API matchingtyping'sget_args/get_origin/get_type_hints.multipledispatch.dispatcher.Dispatchersubclass that is capable of making use of this new functionality and is used by all Funsordispatched_interpretations viafunsor.registry.KeyedRegistryI have also refactored and drastically simplified
FunsorMetausing this machinery. In a followup PR I will do the same forfunsor.domains.ArrayType.Context
After these changes, it should be possible to use some of the Python standard
typinglibrary directly when describing funsor input and output types or when writing patterns, e.g. we could simply replaceProductfrom #430 withtyping.Tuple:...or use
typing.Tuple, including variadic cases, in patterns like the ones needed forFinitaryin #423:As suggested in #355, we could even replace many of the assertions in our
Funsor.__init__methods with runtime type checking in interpreters, eliminating lots of boilerplate code and potentially boosting performance (by making many assertions optional):In #355 I had originally suggested going even further toward integrating
typingwith Funsor interpretations than the changes here by making parametricFunsorsubtypes usetyping.Generic. I am somewhat skeptical about taking that additional step because of how difficult it is to interact withtyping.Genericat runtime, and have chosen in this PR to retain a simplified version of theGenericTypeMetaclass as a substrate forFunsorandFunsorMeta.Tested
Remaining tasks
typing-related dependency issues for Python 3.6, 3.7, 3.8, 3.9issubclasstests intest_terms.pyto a new filetest_typing.pytest_typing.pyTriaged
DispatcherandFunsorMetaimplementations - this appears to be less severe than originally feared, so I am OK with addressing performance in future PRs unless there's a really easy fix.