Giter Site home page Giter Site logo

Comments (2)

chrahunt avatar chrahunt commented on June 20, 2024 2

To get the types we must call typing.get_type_hints and passing the class. This returns a mapping that can be used to retrieve the type by passing field.name. For example:

$ python
Python 3.7.1 (default, Jan  1 2019, 15:19:53)
[GCC 5.5.0 20171010] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from __future__ import annotations
>>> from dataclasses import dataclass, fields
>>> import typing
>>> @dataclass
... class C:
...  i: int
...
>>> fields(C)
(Field(name='i',type='int',default=<dataclasses._MISSING_TYPE object at 0x7f62e8594780>,default_factory=<dataclasses._MISSING_TYPE object at 0x7f62e8594780>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),_field_type=_FIELD),)
>>> fields(C)[0].type
'int'
>>> fields(C)[0].name
'i'
>>> typing.get_type_hints(C)
{'i': <class 'int'>}
>>> typing.get_type_hints(C)[fields(C)[0].name]
<class 'int'>

The only caveat with this approach is that for classes that use forward references (with the class name itself) the typing.get_type_hints will raise a NameError if called before the class has been actually initialized in its module (such as directly in a decorator).

from dataclasses-json.

lidatong avatar lidatong commented on June 20, 2024

Hi, yes, I am aware of this issue and it is being tracked in #31, specifically #5. Thanks for reporting.

My instinct is there isn't a good way to address this (as described in #5) without improvements to the typing API, but let me know if you have suggestions.

from dataclasses-json.

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.