I'm defining a method
def foo_my_class(my_class: ???, bar: str) -> None: """ Operate on my_class """
I wonder, how can I use type hinting feature to specify that class should be passed in the first argument.
Basically, what should I put instead of ???
mark up there?
Here is some more code to be more specific on what I am trying to achieve.
class Base(object): """base class""" class X(Base): """some class""" class Y(Base): """some other class""" foo_my_class(X, "foo") foo_my_class(Y, "bar")
In PHP, we can use type hinting for Object, Array and callable data type.
Type hints improve IDEs and linters. They make it much easier to statically reason about your code. Type hints help you build and maintain a cleaner architecture. The act of writing type hints forces you to think about the types in your program.
They are used to add types to variables, parameters, function arguments as well as their return values, class attributes, and methods. Adding type hints has no runtime effect: these are only hints and are not enforced on their own.
Type hinting is a formal solution to statically indicate the type of a value within your Python code. It was specified in PEP 484 and introduced in Python 3.5. The name: str syntax indicates the name argument should be of type str . The -> syntax indicates the greet() function will return a string.
As explained here, you can use Type:
from typing import Type class X: """some class""" def foo_my_class(my_class: Type[X], bar: str) -> None: """ Operate on my_class """
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With