Giter Site home page Giter Site logo

Comments (5)

sydney-runkle avatar sydney-runkle commented on June 15, 2024

@tamird,

Thanks for reporting this - definitely looks like a typing bug. The concern I have here is that we can't use Sequence as the type hint, as not all sequence types are JSON serializable (consider sets, for example).

Here's one possible alternative:

if TYPE_CHECKING:
    # This seems to only be necessary for mypy
    J = TypeVar('J', bound='JsonValue')
    JsonValue: TypeAlias = Union[
        List[J],
        Dict[str, J],
        str,
        bool,
        int,
        float,
        None,
    ]

Let me know your thoughts on that. I can open a PR to fix this issue if you approve of said approach as well :).

from pydantic.

sydney-runkle avatar sydney-runkle commented on June 15, 2024

One drawback is that this requires homogeneity in terms of the subtype of JsonValue used within a list or dict, which we might not want...

from pydantic.

tamird avatar tamird commented on June 15, 2024

I'm pretty sure sets are not Sequences: https://mypy-play.net/?mypy=latest&python=3.12&gist=f71b466422216f85473dbb9880dd5f5c&flags=warn-unreachable.

What would be the problem with

    JsonValue: TypeAlias = Union[
        Sequence['JsonValue'],
        Mapping[str, 'JsonValue'],
        str,
        bool,
        int,
        float,
        None,
    ]

? Other than this discriminator having to be made more general.

from pydantic.

sydney-runkle avatar sydney-runkle commented on June 15, 2024

@tamird,

Good point about sets. What about deque types? Deques are sequences, but aren't json serializable...

from pydantic.

tamird avatar tamird commented on June 15, 2024

Can you give a runnable example? I don't understand why it isn't possible to serialize any sequence as a JSON array and to deserialize any JSON array as a list.

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.