Handling the Bool
type with type checkers
#2439
Replies: 1 comment 1 reply
-
The decision to subclass primitive types in PythonGenerator has proven controversial. Even though this works for strings, ints, etc, it can cause confusion. When passing instances of these objects to some external tools, they may perform operations like As you've noticed, there is an additional wrinkle with bools, as these can't be subclassed in the same way: https://stackoverflow.com/questions/2172189/why-i-cant-extend-bool-in-python We could explore changing pythongen to simply use the native bools. However, I would recommend moving over to pydanticgen, this should play better with mypy. In pythongen we don't subclass primitives, we just use them directly, for the core types. There are a number of other advantages to pydanticgen. |
Beta Was this translation helpful? Give feedback.
-
The metamodel seems to have a
Bool
type which is weird to work with.It's defined such that calling
Bool()
returns abool
so I suppose an instance of the class can never actually exist. However this doesn't help with type checkers.So, if I want to write a custom generator:
The pyright type checker reports:
I suppose I can do this, but it's a bit unusual:
Beta Was this translation helpful? Give feedback.
All reactions