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

Add a new builtin Spec and conformer for renaming map keys #68

Draft
wants to merge 16 commits into
base: master
Choose a base branch
from

Conversation

chrisrink10
Copy link
Contributor

@chrisrink10 chrisrink10 commented Apr 21, 2020

This PR adds a new "Spec" factory and conformer under s.rename which allows callers to specify simple rename operations as a Spec factory. The Spec returned by this factory performs a simple type check that it's getting a dict (this is configurable by a keyword argument). By default, s.rename also ships with a validator that ensures no keys are overwritten during the rename (this can be disabled by setting the keyword argument overwrite_duplicate_keys=True). The default conformer will perform the rename operation. The intended use case here is as part of an s.all(...) chain with a mapping Spec to follow and validate the renamed keys.

Fixes #60

@chrisrink10 chrisrink10 marked this pull request as ready for review April 28, 2020 13:00
@chrisrink10 chrisrink10 requested review from Palisand and aphera April 28, 2020 13:00
Comment on lines +1208 to +1209
If ``type_`` is specified, the resulting Spec will only validate the input value as
types named by ``type_``, otherwise :py:class:`dict` will be used.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you lost me here

def rename_spec( # noqa: MC0001 # pylint: disable=too-many-arguments
tag: Optional[Tag] = None,
replacements: Optional[Mapping[Hashable, Union[Hashable, List[Hashable]]]] = None,
retain_replaced_keys: bool = False,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it's a big change in a different direction but what if you split this into two specs, rename and copy_value specs. They could reuse this guy under the hood but it might make the api a bit simpler.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would the copy_value spec work?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copy_value_spec would be rename_spec but hwere retain_replaced_keys = True

@chrisrink10 chrisrink10 mentioned this pull request Apr 29, 2020
@chrisrink10 chrisrink10 marked this pull request as draft April 30, 2020 13:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Add a conformer for renaming dict keys
2 participants