Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type Hinting: Argument Of Type Class [duplicate]

Tags:

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") 
like image 600
anti1869 Avatar asked Oct 28 '15 09:10

anti1869


People also ask

Which data types can be declared with type hinting?

In PHP, we can use type hinting for Object, Array and callable data type.

What is the purpose of type hinting?

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.

Is type hinting enforced?

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.

Are type hints Pythonic?

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.


1 Answers

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 """ 
like image 180
roipoussiere Avatar answered Oct 26 '22 08:10

roipoussiere