0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

抽象メソッドの実装が漏れているクラスを定義しただけでは、mypyはエラーを出力しない

Last updated at Posted at 2023-05-03

環境

  • Python 3.11.2
  • mypy 1.2.0

起きたこと

抽象基底クラスAnimalを継承しているCatクラスでは、cryメソッドの実装が漏れていました。

sample.py
import abc


class Animal(abc.ABC):
    @abc.abstractmethod
    def cry(self) -> None:
        pass


class Dog(Animal):
    def cry(self) -> None:
        print("bowwow")

    def only_dog_method(self) -> None:
        print("only dog method")


class Cat(Animal):
    # cryメソッドの実装漏れ

    def only_cat_method(self) -> None:
        print("only cat method")

この状態でmypyを実行してもエラーは発生しません。なぜでしょうか?

$ mypy sample.py 
Success: no issues found in 1 source file

原因

コードを書いている私は、Catクラスを具象クラスとして扱っていましたが、mypyはCatが抽象基底クラスか具象クラスか判断できません。mypyはCatクラスを抽象基底クラスとして扱っているため、エラーは発生しませんでした。

There is one important peculiarity about how ABCs work in Python – whether a particular class is abstract or not is somewhat implicit. In the example below, Derived is treated as an abstract base class since Derived inherits an abstract f method from Base and doesn’t explicitly implement it. The definition of Derived generates no errors from mypy, since it’s a valid ABC:

https://mypy.readthedocs.io/en/stable/class_basics.html#abstract-base-classes-and-multiple-inheritance 引用

解決策

インスタンスを生成すれば、mypyはエラーを出力します。

sample.py
...

dog = Dog()
cat = Cat()
$ mypy sample.py 
sample.py:26: error: Cannot instantiate abstract class "Cat" with abstract attribute "cry"  [abstract]
Found 1 error in 1 file (checked 1 source file)

もちろん、Python実行時にエラーも発生します。

$ python sample.py
Traceback (most recent call last):
  File "/home/vagrant/Documents/study/20230503/sample.py", line 26, in <module>
    cat = Cat()
          ^^^^^
TypeError: Can't instantiate abstract class Cat with abstract method cry
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?