Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Narrow type of argmap from Mapping to Dict #682

Merged
merged 1 commit into from
Jan 11, 2022

Conversation

sirosen
Copy link
Collaborator

@sirosen sirosen commented Jan 5, 2022

The Mapping case is meant to support passthrough to Schema.from_dict. However, only Dict is actually supported.
Therefore, narrowing this from Mapping to Dict makes the annotation more accurate.

In terms of runtime behavior, the check is changed from looking at isinstance(argmap, collections.abc.Mapping) to isinstance(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

The `Mapping` case is meant to support passthrough to
`Schema.from_dict`. However, only `Dict` is actually supported.
Therefore, narrowing this from `Mapping` to `Dict` makes the
annotation more accurate.

In terms of runtime behavior, the check is changed from looking at
`isinstance(argmap, collections.abc.Mapping)` to
`isinstance(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.
@lafrech lafrech merged commit 1193948 into marshmallow-code:dev Jan 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The type of "argmap" allows for Mapping[str, Field], but Schema.from_dict only supports Dict[str, Field]
2 participants