Python Forum
Bug ? when dataclass field name == field type
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Bug ? when dataclass field name == field type
#1
Hi,
When I create a dataclass with same field.name and field.type and assign a default value to None typehint get lost. I know that it does not respect naming convention but I have reasons to do so in my particular case.

Standard Behaviour
from dataclasses import dataclass
import typing
from typing import Optional, List, Tuple

@dataclass
class NotBuiltIn:
	test: str

@dataclass
class MyDataClass:
	field: Optional[NotBuiltIn] = None

typing.get_type_hints(MyDataClass)
Output:
{'field': typing.Union[__main__.NotBuiltIn, NoneType]}
Type hint get lost (variable name == type name) :
@dataclass
class layers:
	...

@dataclass
class material:
    layers: Optional[layers] = None

typing.get_type_hints(material)
Output:
{'layers': NoneType}
It looks like a bug but is it ?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  UPDATE SQLITE TABLE - Copy a fields content to another field. andrewarles 14 563 May-08-2021, 04:58 PM
Last Post: ibreeden
  drf ordering by custom field and add ranking in to_representation tomfong521 0 256 Mar-24-2021, 09:56 AM
Last Post: tomfong521
  finding element of specific field in pandas adjacency matrix amjass12 0 335 Oct-27-2020, 09:27 AM
Last Post: amjass12
  find a string in a field in MongoDB Leon79 2 630 Jul-19-2020, 09:20 PM
Last Post: menator01
  Find field name from all possible values kashcode 1 564 Apr-22-2020, 06:14 PM
Last Post: deanhystad
  Field with dynamic id not found at playback pythonscripting 0 497 Feb-20-2020, 02:43 PM
Last Post: pythonscripting
  Add path to a local file in pop-up field pythonscripting 1 648 Feb-08-2020, 10:57 PM
Last Post: Larz60+
  Creating a set with dataclass and dict hobbitdur 11 2,998 Jan-16-2020, 11:22 AM
Last Post: buran
  Type hinting - return type based on parameter micseydel 2 895 Jan-14-2020, 01:20 AM
Last Post: micseydel
  text box field vishnu 4 845 Oct-04-2019, 06:38 AM
Last Post: vishnu

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020