If I have a pydantic class like:
from typing import Annotated, get_origin
from pydantic import BaseModel
class IgnorableBaseModel(BaseModel):
_ignored: ClassVar[dict[str, Any]] = {}
def __getattr__(self, attr):
"""do something with _ignored, else fallback to default"""
def __init_subclass__(cls, **kwargs) -> None:
del_keys = []
for key, annotation in cls.__annotations__.items():
if key.startswith("_"): # exclude protected/private attributes
continue
if get_origin(annotation) is Annotated:
if get_args(annotation)[1] == "ignore me"
cls._ignored[key] = cls.__annotations__[key]
del_keys.append(key)
for key in del_keys:
del cls.__annotations__[key]
class MyClass(IgnorableBaseModel):
name: Annotated[str, "ignore me"] # ignore this
x: int
I am using this name for a variable that is defined during the __init__ and accessed via __getattr__ so I can't set a value for it. Is there a way to tell mypy to ignore this, or do I need to always override the init args like:
class MyClass(IgnorableBaseModel):
name: Annotated[str, "ignore me"]
x: int
def __init__(self, x: int):
self.x = x
It'd be great if there was an annotation I could add that would inform mypy that this variable was not needed during the init.
"ignore me" is used here to indicate that I don't want to see the error:
Missing named argument "name" for "MyClass"Mypycall-arg
To provide some background, I'm trying to make a python DSL and so it helps to be able to have some attributes type hinted, but not actually require values.
If you want
nameto be defined by__init__without having to pass its value as an argument, use theinitargument toField.This can also be written as
Now the autogenerated
__init__method will only expect an argument forx, and it will be up to use to ensure thenameattribute on an instance ofMyClassgets defined. (This could be by using a default value, or a default factory, or an explicitly defined__init__method, or some other technique that Pydantic provides.)