Python Forum

Full Version: Bug ? when dataclass field name == field type
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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 ?