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
  Plotting streamlines of the velocity field within an ice sheet verhaegenyoni 1 311 Mar-23-2024, 10:09 PM
Last Post: verhaegenyoni
  append str to list in dataclass flash77 6 343 Mar-14-2024, 06:26 PM
Last Post: flash77
  parse json field from csv file lebossejames 4 668 Nov-14-2023, 11:34 PM
Last Post: snippsat
  Pywinauto typing in the wrong field EGameiro 0 656 Jun-07-2023, 10:01 PM
Last Post: EGameiro
  Display value in the another entry field instead of message box adisc 6 1,504 Jun-25-2022, 02:30 PM
Last Post: rob101
  magic related field in Django model sonh 1 1,201 Apr-24-2022, 12:37 PM
Last Post: sonh
  How to get datetime from numeric format field klllmmm 3 1,961 Nov-06-2021, 03:26 PM
Last Post: snippsat
  Auto increament in Entry field. in tkinter GUI cybertooth 7 4,012 Sep-17-2021, 07:56 AM
Last Post: cybertooth
  Display table field on multiple lines, 'wordwrap' 3python 0 1,747 Aug-06-2021, 08:17 PM
Last Post: 3python
  UPDATE SQLITE TABLE - Copy a fields content to another field. andrewarles 14 4,246 May-08-2021, 04:58 PM
Last Post: ibreeden

Forum Jump:

User Panel Messages

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