Giter Site home page Giter Site logo

support NewType about pydantic HOT 4 CLOSED

pydantic avatar pydantic commented on May 22, 2024
support NewType

from pydantic.

Comments (4)

samuelcolvin avatar samuelcolvin commented on May 22, 2024

Surely simpler to construct the class properly:

class BossId(str):
    pass

That way you can easily add custom validation for BossIds 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.

TimSimpsonR avatar TimSimpsonR commented on May 22, 2024

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.

samuelcolvin avatar samuelcolvin commented on May 22, 2024

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.

samuelcolvin avatar samuelcolvin commented on May 22, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.