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

Bytesize #977

Merged
merged 13 commits into from Nov 9, 2019
Merged

Bytesize #977

merged 13 commits into from Nov 9, 2019

Conversation

dgasmith
Copy link
Contributor

@dgasmith dgasmith commented Nov 7, 2019

Change Summary

Adds a ByteSize object to automatically cast common byte string to bytes.

Related issue number

#971

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)

@codecov
Copy link

codecov bot commented Nov 7, 2019

Codecov Report

Merging #977 into master will not change coverage.
The diff coverage is 100%.

@@          Coverage Diff          @@
##           master   #977   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files          19     19           
  Lines        3237   3297   +60     
  Branches      642    654   +12     
=====================================
+ Hits         3237   3297   +60
Impacted Files Coverage Δ
pydantic/types.py 100% <100%> (ø) ⬆️
pydantic/errors.py 100% <100%> (ø) ⬆️
pydantic/dataclasses.py 100% <0%> (ø) ⬆️

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 4218ff7...f5c82b4. Read the comment docs.

docs/usage/types.md Show resolved Hide resolved
@@ -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):
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
class InvalidByteString(PydanticValueError):
class InvalidByteSize(PydanticValueError):

and below

pydantic/types.py Show resolved Hide resolved
pydantic/types.py Outdated Show resolved Hide resolved
pydantic/types.py Outdated Show resolved Hide resolved
pydantic/types.py Outdated Show resolved Hide resolved
unit = "B"

try:
unit_mult = BYTE_SIZES[unit.lower()]
Copy link
Member

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".

Copy link
Contributor Author

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.

Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added.

pydantic/types.py Outdated Show resolved Hide resolved
pydantic/types.py Outdated Show resolved Hide resolved
pydantic/types.py Outdated Show resolved Hide resolved
pydantic/types.py Outdated Show resolved Hide resolved
dgasmith and others added 8 commits November 8, 2019 09:22
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
@samuelcolvin
Copy link
Member

can you fix tests please. I think it's just double quotes.

Best to run make test before pushing.

docs/usage/types.md Outdated Show resolved Hide resolved
docs/examples/types_bytesize.py Outdated Show resolved Hide resolved
pydantic/types.py Outdated Show resolved Hide resolved
pydantic/types.py Outdated Show resolved Hide resolved
'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})
Copy link
Member

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.

@dgasmith dgasmith force-pushed the bytesize branch 2 times, most recently from 6e98bb7 to 9d2c3be Compare November 8, 2019 15:33
@samuelcolvin samuelcolvin merged commit b92e74d into pydantic:master Nov 9, 2019
@samuelcolvin
Copy link
Member

thanks so much.

@samuelcolvin
Copy link
Member

samuelcolvin commented Nov 9, 2019

ID of this matches my favourite xkcd

@dgasmith dgasmith deleted the bytesize branch November 11, 2019 01:56
andreshndz pushed a commit to cuenca-mx/pydantic that referenced this pull request Jan 17, 2020
* 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
alexdrydew pushed a commit to alexdrydew/pydantic that referenced this pull request Dec 23, 2023
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.

None yet

2 participants