OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to annotate a type that's a class object (instead of a class instance)?

  • Thread starter Thread starter Gomes J. A.
  • Start date Start date
G

Gomes J. A.

Guest
What is the proper way to annotate a function argument that expects a class object instead of an instance of that class?

In the example below, some_class argument is expected to be a type instance (which is a class), but the problem here is that type is too broad:

Code:
def construct(some_class: type, related_data:Dict[str, Any]) -> Any:
    ...

In the case where some_class expects a specific set of types objects, using type does not help at all. The typing module might be in need of a Class generic that does this:

Code:
def construct(some_class: Class[Union[Foo, Bar, Baz]], related_data:Dict[str, Any]) -> Union[Foo, Bar, Baz]:
    ...

In the example above, some_class is the Foo, Bar or Faz class, not an instance of it. It should not matter their positions in the class tree because some_class: Class[Foo] should also be a valid case. Therefore,

Code:
# classes are callable, so it is OK
inst = some_class(**related_data)

or

Code:
# instances does not have __name__
clsname = some_class.__name__

or

Code:
# an operation that only Foo, Bar and Baz can perform.
some_class.a_common_classmethod()

should be OK to mypy, pytype, PyCharm, etc.

How can this be done with current implementation (Python 3.6 or earlier)?
<p>What is the proper way to annotate a function argument that expects a class object instead of an instance of that class?</p>
<p>In the example below, <code>some_class</code> argument is expected to be a type instance (which is a class), but the problem here is that <code>type</code> is too broad:</p>
<pre><code>def construct(some_class: type, related_data:Dict[str, Any]) -> Any:
...
</code></pre>
<p>In the case where <code>some_class</code> expects a specific set of types objects, using <code>type</code> does not help at all. The <code>typing</code> module might be in need of a Class generic that does this:</p>
<pre><code>def construct(some_class: Class[Union[Foo, Bar, Baz]], related_data:Dict[str, Any]) -> Union[Foo, Bar, Baz]:
...
</code></pre>
<p>In the example above, <code>some_class</code> is the <code>Foo</code>, <code>Bar</code> or <code>Faz</code> class, not an instance of it. It should not matter their positions in the class tree because <code>some_class: Class[Foo]</code> should also be a valid case. Therefore,</p>
<pre><code># classes are callable, so it is OK
inst = some_class(**related_data)
</code></pre>
<p>or</p>
<pre><code># instances does not have __name__
clsname = some_class.__name__
</code></pre>
<p>or</p>
<pre><code># an operation that only Foo, Bar and Baz can perform.
some_class.a_common_classmethod()
</code></pre>
<p>should be OK to mypy, pytype, PyCharm, etc.</p>
<p>How can this be done with current implementation (Python 3.6 or earlier)?</p>
 

Latest posts

Top