Couldn’t seem to find a definitive answer. I want to do a type hint for a function and the type being some custom class that I have defined, called it CustomClass().
And then let’s say in some function, call it FuncA(arg), I have one argument named arg. Would the correct way to type hint FuncA be:
def FuncA(arg: CustomClass):
Or would it be:
from typing import Type def FuncA(Arg:Type[CustomClass]):
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
The former is correct, if arg accepts an instance of CustomClass:
def FuncA(arg: CustomClass):
# ^ instance of CustomClass
In case you want the class CustomClass itself (or a subtype), then you should write:
from typing import Type # you have to import Type
def FuncA(arg: Type[CustomClass]):
# ^ CustomClass (class object) itself
Like it is written in the documentation about Typing:
<b>class typing.Type(Generic[CT_co])</b>A variable annotated with
Cmay accept a value of typeC. In
contrast, a variable annotated withType[C]may accept values that
are classes themselves – specifically, it will accept the class
object ofC.
The documentation includes an example with the int class:
a = 3 # Has type 'int' b = int # Has type 'Type[int]' c = type(a) # Also has type 'Type[int]'
Method 2
Willem Van Onsem’s answer is of course correct, but I’d like to offer a small update. In PEP 585, type hinting generics were introduced in standard collections. For example, whereas we previously had to say e.g.
from typing import Dict
foo: Dict[str, str] = { "bar": "baz" }
we can now forgo the parallel type hierarchy in the typing module and simply say
foo: dict[str, str] = { "bar": "baz" }
This feature is available in python 3.9+, and also in 3.7+ if using from __future__ import annotations.
In terms of this specific question, it means that instead of from typing import Type, we can now simply annotate classes using the built-in type:
def FuncA(arg: type[CustomClass]):
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0