Giter VIP home page Giter VIP logo

datadict's Issues

PEP 681 support: Data Class Transforms

I needed a dict-like dataclass and was going to implement it myself, but I did some Googling and found that your implementation does the trick. Thanks!

One minor issue I found is that your "dataclass-like" decorator isn't understood by type checkers like mypy and pyright as of now. This is a known limitation of user-defined dataclass-like functions, described and fixed in PEP 681. The error given by pyright is "Untyped class decorator obscures type of class; ignoring decorator".

If you're up for it, I can submit a Pull Request implementing this change. I recommend the "simple fix" below, which adds a dependency to typing_extensions (which itself requires Python 3.7 at minimum). But there's also a fix that doesn't add any dependencies, though it will bump your minimum supported Python to 3.8.

Simple fix and an added dependency (dependency needs Python 3.7+)

The fix will be available in the standard library in 3.11, but is available today through typing_extensions. Your package could be updated to support PEP 681 by adding the typing_extensions dependency and declaring the import:

from typing_extensions import dataclass_transform

And changing this:

def dataclass(cls=None, **kwargs):

to this:

@dataclass_transform()
 def dataclass(cls=None, **kwargs): 

Since 3.11 will have dataclass_transform built in, typing_extensions conditionally chooses its own implementation for pre 3.11, and imports the built-in for 3.11 onward.

More involved fix and no added dependencies (using Python 3.8+ protocols)

See the "Enforcing dataclasses" section of this StackOverflow answer.

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.