LoginSignup
1
2

More than 3 years have passed since last update.

type comment 形式の変数アノテーションでは ignore 型が記述できない

Posted at

とてもどうでもいいことに気づきました。

実際に試してみた

これが

class ignore:
    pass


class Ignore:
    pass


foo = 1  # type: ignore
bar = 1  # type: Ignore

こうじゃ

$ mypy test.py
test.py:10: error: Incompatible types in assignment (expression has type "int", variable has type "Ignore")
Found 1 error in 1 file (checked 1 source file)

foo はエラーになりません。

変数アノテーションとして書くとうまく動く

class ignore:
    pass


class Ignore:
    pass


foo: ignore = 1
bar: Ignore = 1
$ mypy test.py
test.py:9: error: Incompatible types in assignment (expression has type "int", variable has type "ignore")
test.py:10: error: Incompatible types in assignment (expression has type "int", variable has type "Ignore")
Found 2 errors in 1 file (checked 1 source file)

まとめ

type_comment 形式の型アノテーションはもう使うのをやめましょう。

1
2
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
1
2