# Mypy: "Class cannot subclass (has type "Any")"

**URL:** <https://discuss.python.org/t/mypy-class-cannot-subclass-has-type-any/70549>\
**Category:** Python Help\
**Tags:** typing\
**Created:** [November 8, 2024, 1:30pm UTC](https://discuss.python.org/t/mypy-class-cannot-subclass-has-type-any/70549 "2024-11-08T13:30:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jacopoabramo](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jacopoabramo/32/23840_2.png) [@jacopoabramo](https://discuss.python.org/u/jacopoabramo)\
**Post date:** [November 8, 2024, 1:30pm UTC](https://discuss.python.org/t/mypy-class-cannot-subclass-has-type-any/70549/1 "2024-11-08T13:30:54Z")

</div>

Greetings! It’s my very first topic on this forum so I apologize in advance if this has already been answered somewhere else.

EDIT: forgot to mention:

- OS: Windows 11
- Python: 3.10.15

I’m developing a software which is divided into two separately shipped packages:

- a toolkit package;
- a core package.

This is because the toolkit gives the opportunity to other contributors to build plugins to attach to the core.

In the toolkit I have the following classes (I’m just leaving the class declaration since the content is not relevant - I think):

> toolkit\log.py

```python
from abc import ABC

class Loggable(ABC):
    ...

```

> toolkit\handler.py

```python
from toolkit.log import Loggable
from abc import ABCMeta

class Handler(Loggable, metaclass=ABCMeta):
    ...

```

The `Loggable` class provides a standard interface to provide log messages which are written in the core logger.  
The `Handler` class is an example implementation of how to use the `Loggable` class to extend another abstract class.  
While writing this topic I realize that `Loggable` doesn’t have to be abstract as well for my purposes but for now I’ll just leave it at that.

In the core package, I have an implementation of `Handler`:

> core\handler.py

```python
from toolkit.handler import Handler

class EngineHandler(Handler):
    ...

```

Now when running `mypy` on the core package, I get the following error:

```t
Class cannot subclass "Handler" (has type "Any")

```

What exactly is going on here? Why is it complaining about this inheritance? Is it related to the content of the `Handler` or `Loggable` classes somehow?

---

<div class="post-metadata">

**Author:** ![bschubert](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/bschubert/32/26920_2.png) [@bschubert](https://discuss.python.org/u/bschubert)\
**Post date:** [November 8, 2024, 1:52pm UTC](https://discuss.python.org/t/mypy-class-cannot-subclass-has-type-any/70549/2 "2024-11-08T13:52:21Z")

</div>

Hi Jacopo! Most likely mypy isn’t able to follow the import to `toolkit`, which causes `Handler` to have an inferred type of `Any`. That often happens for one of two reasons: 1) `toolkit` isn’t installed in the environment that mypy is running in (or otherwise added to the mypy path), or 2) toolkit doesn’t expose PEP 561 typing information (usually due to a missing `py.typed` marker file).

Usually, there will be other errors in mypy’s output that will hint at what’s going on (unless you suppress them with e.g. a `# type: ignore` on the import or setting `ignore_missing_imports = true` in your config file).

See also [Creating PEP 561 compatible packages](https://mypy.readthedocs.io/en/stable/installed_packages.html#creating-pep-561-compatible-packages) from the mypy docs and [Packaging Type Information](https://peps.python.org/pep-0561/#packaging-type-information) from PEP 561 for more details on including type information with your package.

---

<div class="post-metadata">

**Author:** ![jacopoabramo](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jacopoabramo/32/23840_2.png) [@jacopoabramo](https://discuss.python.org/u/jacopoabramo)\
**Post date:** [November 8, 2024, 1:54pm UTC](https://discuss.python.org/t/mypy-class-cannot-subclass-has-type-any/70549/3 "2024-11-08T13:54:06Z")

</div>

@bschubert thank you for the swift reply. I’m still new to proper packaging techniques so this is very insightful for me. I’ll have a look and try to see how to solve it then. Much appreciate it!

EDIT: it worked. For other’s who may encounter this thread, this is what I did:

- added `py.typed` to my package main directory (in my case `src\toolkit\py.typed`)
- added `py.typed` in `MANIFEST.in` with
  - `include toolkit\py.typed`

- I use `pyproject.toml`, so I added the package data option:

```t
[tool.setuptools.package-data]
"toolkit" = ["py.typed"]

```
