File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change @@ -72,6 +72,32 @@ flagged as an error.
7272 e.g. the :py:func: `pow ` builtin returns ``Any `` (see `typeshed issue 285
7373 <https://github.com/python/typeshed/issues/285> `_ for the reason).
7474
75+ - **:py:meth:`__init__ <object.__init__>` method has no annotated
76+ arguments or return type annotation. ** :py:meth: `__init__ <object.__init__> `
77+ is considered fully-annotated **if at least one argument is annotated **,
78+ while mypy will infer the return type as ``None ``.
79+ The implication is that, for a :py:meth: `__init__ <object.__init__> ` method
80+ that has no argument, you'll have to explicitly annotate the return type
81+ as ``None `` to type-check this :py:meth: `__init__ <object.__init__> ` method:
82+
83+ .. code-block :: python
84+
85+ def foo (s : str ) -> str :
86+ return s
87+
88+ class A ():
89+ def __init__ (self , value : str ): # Return type inferred as None, considered as typed method
90+ self .value = value
91+ foo(1 ) # error: Argument 1 to "foo" has incompatible type "int"; expected "str"
92+
93+ class B ():
94+ def __init__ (self ): # No argument is annotated, considered as untyped method
95+ foo(1 ) # No error!
96+
97+ class C ():
98+ def __init__ (self ) -> None : # Must specify return type to type-check
99+ foo(1 ) # error: Argument 1 to "foo" has incompatible type "int"; expected "str"
100+
75101 - **Some imports may be silently ignored **. Another source of
76102 unexpected ``Any `` values are the :option: `--ignore-missing-imports
77103 <mypy --ignore-missing-imports> ` and :option: `--follow-imports=skip
You can’t perform that action at this time.
0 commit comments