Can't figure out the syntax for Optional[T] to use with dataclasses #2536
-
From what I gather this looks correct: (defclass [dataclass] MyClass []
"A dataclass to hold round information"
#^ (of Optional int) myattr None) If I evaluate that it evaluates with no issue if I make |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
What would be the Python equivalent of the desired Hy code? |
Beta Was this translation helpful? Give feedback.
-
# => (defclass [dataclass] MyClass []
# ... "A dataclass to hold round information"
# ... #^ (of Optional int) myattr None)
@dataclass
class MyClass:
"""A dataclass to hold round information"""
myattr: Optional[int]
None You probably want this: (defclass [dataclass] MyClass []
(setv #^ (of Optional int) myattr None)) which translates to this: @dataclass
class MyClass:
myattr: Optional[int] = None |
Beta Was this translation helpful? Give feedback.
hy --spy
(orhy2py
) is your friend!The
#^
macro doesn't generate an assignment, it's just the annotation; you still need asetv
. So your current Hy code declares a fieldmyattr
with no assignment, followed by a standalone expressionNone
:You probably want this:
which translates to this: