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
Question how to type hint a function in a dataclass? Calab 3 721 Feb-27-2025, 04:40 AM
Last Post: Calab
  Entry field random pull from list, each return own line Bear1981 6 686 Feb-25-2025, 06:09 AM
Last Post: Pedroski55
  change dataclass to frozen at runtime jpanico 2 1,015 Oct-06-2024, 09:38 AM
Last Post: snippsat
  Plotting streamlines of the velocity field within an ice sheet verhaegenyoni 1 1,338 Mar-23-2024, 10:09 PM
Last Post: verhaegenyoni
  append str to list in dataclass flash77 6 3,258 Mar-14-2024, 06:26 PM
Last Post: flash77
  parse json field from csv file lebossejames 4 1,955 Nov-14-2023, 11:34 PM
Last Post: snippsat
  Pywinauto typing in the wrong field EGameiro 0 1,191 Jun-07-2023, 10:01 PM
Last Post: EGameiro
  Display value in the another entry field instead of message box adisc 6 2,648 Jun-25-2022, 02:30 PM
Last Post: rob101
  magic related field in Django model sonh 1 1,909 Apr-24-2022, 12:37 PM
Last Post: sonh
  How to get datetime from numeric format field klllmmm 3 2,678 Nov-06-2021, 03:26 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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