Comments (4)
Surely simpler to construct the class properly:
class BossId(str):
pass
That way you can easily add custom validation for BossId
s etc.
If you really don't want to do that, why not use:
BossId = type('BossId', str, {})
?
If for some reason this really doesn't work, I'd be happy to accept a pull request for your fix above. But we might also need extra logic so that DirectReport.boss
is an instance of BossId
not just a str
. How would this work?
from pydantic.
Hmm, good point that class Type(str)
could be a valid work around. I might consider that for string types that require validation or if I need the type info to be attached to the value at runtime.
NewType is designed to be cheap; while MyPy validates it, at runtime it's only a function call, and the instances it creates are the underlying type. So I think it would be ok if Pydantic treated BossId
as just a str
too.
from pydantic.
humm, ok but I bet you someone in future will come back and whinge when they get confused about the type of DirectReport.boss
.
Since your change above should have no effect on performance for other usage I'd be happy to accept a pull request for this change provided it has full coverage and documentation.
from pydantic.
Happy to accept a PR which supports this provided it's not to complicated.
CustomerId
should be validated with the standard str
validation logic.
from pydantic.
Related Issues (20)
- Passing data to nested models HOT 1
- Dependencies missing when installing pydantic 2.7.1 using Poetry via legacy PyPI API https://pypi.org/simple HOT 4
- Openapi.json does not support pydantic v1 and pydantic v2 at the same time
- Unexpected behavior: The cached computed field will appear in __dict__ after the first call HOT 2
- Ai
- Allow `model_validate_json` to return valid JSON string upon raising error HOT 3
- case_sensitive doesn't work in optional nested Settings models
- mypy "Explicit "Any" is not allowed" error when extending `BaseModel` HOT 1
- TypeError: typing.ClassVar[int] is not valid as type argument HOT 3
- Enum construction from float worked in v2.6.4 but fails in v2.7.1 HOT 1
- cannot import name 'Optional' from 'pydantic' HOT 1
- Fields with BaseModel type that contain an instance of a model that inherits from BaseModel do not get serialized correctly HOT 3
- Pydantic and Pydantic Settings don't seem to provide library stubs for MyPy HOT 2
- StringConstraints: Bug HOT 3
- misleading error message "Input should be a valid dictionary or instance" when using both pydantic `dataclass` and `BaseModel`
- Class properties have a potential for namespace collision HOT 6
- Default values not validated HOT 12
- Can't pickle generics in certain cases
- typing.Self not supported in generic fields
- Serializing computed fields returning enums HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pydantic.