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
Bytesize #977
Bytesize #977
Conversation
Codecov Report
@@ Coverage Diff @@
## master #977 +/- ##
=====================================
Coverage 100% 100%
=====================================
Files 19 19
Lines 3237 3297 +60
Branches 642 654 +12
=====================================
+ Hits 3237 3297 +60
Continue to review full report at Codecov.
|
pydantic/errors.py
Outdated
@@ -499,3 +501,11 @@ class LuhnValidationError(PydanticValueError): | |||
class InvalidLengthForBrand(PydanticValueError): | |||
code = 'payment_card_number.invalid_length_for_brand' | |||
msg_template = 'Length for a {brand} card must be {required_length}' | |||
|
|||
|
|||
class InvalidByteString(PydanticValueError): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
class InvalidByteString(PydanticValueError): | |
class InvalidByteSize(PydanticValueError): |
and below
unit = "B" | ||
|
||
try: | ||
unit_mult = BYTE_SIZES[unit.lower()] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
are we sure about the lower here?
1b
generally means "1 bit" not "1 byte".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hard to say, I see it quite a few cases where it is stated as 1gb
meaning 1GB
, but your bits comment is very valid. I would say casting lower is fine for my domain, but network engineers would likely be less than happy about this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's keep it like this. It's called ByteSize
after call.
But please add an info
section to the docs explaining this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added.
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
can you fix tests please. I think it's just double quotes. Best to run |
pydantic/types.py
Outdated
'EiB': 2 ** 60, | ||
} | ||
BYTE_SIZES.update({k.lower(): v for k, v in BYTE_SIZES.items()}) | ||
BYTE_SIZES.update({k.lower()[0]: v for k, v in BYTE_SIZES.items() if "i" not in k}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe add a test for this case.
6e98bb7
to
9d2c3be
Compare
thanks so much. |
ID of this matches my favourite xkcd |
* ByteSize object * Human readable format * Lint, tests, and docs * Update pydantic/errors.py Co-Authored-By: Samuel Colvin <samcolvin@gmail.com> * Update pydantic/types.py Co-Authored-By: Samuel Colvin <samcolvin@gmail.com> * Update pydantic/types.py Co-Authored-By: Samuel Colvin <samcolvin@gmail.com> * Allows human readable decimal descriptor * Review comments * Less verbose testing * ByteSize docs * Reworks valid units * Missing type * tweak tests
Change Summary
Adds a
ByteSize
object to automatically cast common byte string to bytes.Related issue number
#971
Checklist
changes/<pull request or issue id>-<github username>.md
file added describing change(see changes/README.md for details)