J
J
Jekson2021-10-11 18:53:35
Python
Jekson, 2021-10-11 18:53:35

Correct use of type annotation?

There is a paydantic class with a default variable value defined in it

class AdUrlMixin(BaseModel):
    url: Optional[HttpUrl] = None

class CommonMixin(AdUrlMixin):
    .......


Further in the logic, the url variable is passed to several consecutive calls

def create_ad(request: HttpRequest, request_body: CommonMixin):
    ....
    result = get_result(request_body.id, request_body.url)


def get_resutl(id: str, url=Optional[HttpUrl] ):
    ....
    get_any_result(id, url)

def get_any_result(id: str, url=Optional[HttpUrl]):
    if url return url else None


The question is, is it necessary to specify the default value (None) in the signature of the called functions (as well as in the passive model) or is it enough to just define the type?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andy_U, 2021-10-11
@Lepilov

If you want to call these functions with one first parameter, then yes, you should. And if always with two - it is not necessary. There are no annotations here.
PS And your function signatures are wrong... There colons are needed instead of =.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question