8000 Added `StrictBytes` type by rlizzo · Pull Request #2136 · pydantic/pydantic · GitHub
[go: up one dir, main page]

Skip to content

Conversation

@rlizzo
Copy link
Contributor
@rlizzo rlizzo commented Nov 19, 2020

Change Summary

Added StrictBytes type which rejects objects which are castable to bytes (ie. int, float, str), but which are not natively bytes type. All changes are modeled after StrictStr, StrictInt, StrictFloat, and StrictBool types.

  • Added strict option to ConstrainedBytes class. Default value of the option (False) respects downstream code continuity. Any initialization or subclass of ConstrianedBytes behaves identically to prior implementations unless the "strict" option is explicitly set to True).

  • Added strict_bytes_validator which rejects any object which is not Union[bytes, bytearray] type. Because bytearray instances are just mutable versions of bytes, we accept both as equivalent native byte types. This is noted explicitly since the builtin bytearray construtor creates objects with the following behavior:

>>> isinstance(bytearray('foo', 'utf-8'), bytes)
False

Related issue number

n/a

Checklist

  • Unit tests for the changes exist
  • Tests pass on CI and coverage remains at 100%
  • Documentation reflects the changes where applicable
  • changes/<pull request or issue id>-<github username>.md file added describing change
    (see changes/README.md for details)

@rlizzo
Copy link
Contributor Author
rlizzo commented Nov 19, 2020

cc @hhsecond

@codecov
Copy link
codecov bot commented Nov 19, 2020

Codecov Report

Merging #2136 (a871ae1) into master (31bc243) will decrease coverage by 0.36%.
The diff coverage is 100.00%.

@@             Coverage Diff             @@
##            master    #2136      +/-   ##
===========================================
- Coverage   100.00%   99.63%   -0.37%     
===========================================
  Files           21       21              
  Lines         4093     4121      +28     
  Branches       823      829       +6     
===========================================
+ Hits          4093     4106      +13     
- Misses           0       12      +12     
- Partials         0        3       +3     
Impacted Files Coverage Δ
pydantic/types.py 100.00% <100.00%> (ø)
pydantic/validators.py 100.00% <100.00%> (ø)
pydantic/version.py 88.23% <0.00%> (-11.77%) ⬇️
pydantic/typing.py 94.30% <0.00%> (-5.70%) ⬇️
pydantic/env_settings.py 97.93% <0.00%> (-2.07%) ⬇️
pydantic/networks.py 99.22% <0.00%> (-0.78%) ⬇️
pydantic/utils.py 99.29% <0.00%> (-0.71%) ⬇️
pydantic/json.py 100.00% <0.00%> (ø)
pydantic/fields.py 100.00% <0.00%> (ø)
... and 2 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 31bc243...a871ae1. Read the comment docs.

Copy link
Member
@samuelcolvin samuelcolvin left a comment

Choose a reason for hiding this comment

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

LGTM, @PrettyWood are you happy with this? If so feel free to merge.

@rlizzo
Copy link
Contributor Author
rlizzo commented Dec 1, 2020

Thanks for the review @samuelcolvin and @PrettyWood!

@PrettyWood PrettyWood merged commit 465f267 into pydantic:master Dec 1, 2020
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.

3 participants

0