Narrow type of argmap from Mapping to Dict #682
Merged
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.
The
Mapping
case is meant to support passthrough toSchema.from_dict
. However, onlyDict
is actually supported.Therefore, narrowing this from
Mapping
toDict
makes the annotation more accurate.In terms of runtime behavior, the check is changed from looking at
isinstance(argmap, collections.abc.Mapping)
toisinstance(argmap, dict)
. This should not impact any users and harmonizes the logic with the declared types and documentation.Technically, it is possible to make a non-dict mapping type which is close enough to dict to be acceptable for
Schema.from_dict
. Such a usage is unlikely and was not supported intentionally in the first place.To avoid unclear and surprising behaviors, non-dict mapping types will now trigger a
TypeError
. The test case for this uses a UserDict, which is dict-like but not a subclass of dict, to demonstrate the sort of case this will catch.Additionally, fix some minor issues with some of the type annotations. In order for a decorator to preserve the type of the decorated function, it needs to be annotated with a type variable. There were a couple of cases of this in
Parser
which have been fixed.resolves #679