From 82913aa32faf29b105bad3dcbb3edfac998fd7c8 Mon Sep 17 00:00:00 2001 From: Tan Yuanhong Date: Sun, 19 Jan 2020 15:48:06 +0800 Subject: [PATCH 1/3] Mention __init__ without arguments in misc... - common issues and solutions -> No errors reported for obviously wrong code --- docs/source/common_issues.rst | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/docs/source/common_issues.rst b/docs/source/common_issues.rst index 14369e44cc56..a8c8ac2c3ee3 100644 --- a/docs/source/common_issues.rst +++ b/docs/source/common_issues.rst @@ -72,6 +72,33 @@ flagged as an error. e.g. the :py:func:`pow` builtin returns ``Any`` (see `typeshed issue 285 `_ for the reason). +- **:py:meth:`__init__ ` method has no annotated + arguments or return type annotation.** :py:meth:`__init__ ` + is considered fully-annotated **if at least one argument is annotated**, + while mypy will infer the return type as 'None'. + The implication is that, for a :py:meth:`__init__ ` method + that has no argument, you'll have to explicitly annotate the return type + as 'None' to type-check this :py:meth:`__init__ ` method: + + .. code-block:: python + def foo(s: str) -> str: + return s + + class A(): + def __init__(self, value: str): # Return type inferred as None, considered as typed method + self.value = value + foo(1) # error: Argument 1 to "foo" has incompatible type "int"; expected "str" + + class B(): + def __init__(self): # No argument is annotated, considered as untyped method + foo(1) # No error! + + class C(): + def __init__(self) -> None: # Must specify return type to type-check + foo(1) # error: Argument 1 to "foo" has incompatible type "int"; expected "str" + + + - **Some imports may be silently ignored**. Another source of unexpected ``Any`` values are the :option:`--ignore-missing-imports ` and :option:`--follow-imports=skip From cc6c0ca04576b33f1bbf8e4606f58ab53b22c537 Mon Sep 17 00:00:00 2001 From: Tan Yuanhong Date: Sun, 19 Jan 2020 16:16:49 +0800 Subject: [PATCH 2/3] Fix build error --- docs/source/common_issues.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/source/common_issues.rst b/docs/source/common_issues.rst index a8c8ac2c3ee3..0bfaeb1243bb 100644 --- a/docs/source/common_issues.rst +++ b/docs/source/common_issues.rst @@ -81,6 +81,7 @@ flagged as an error. as 'None' to type-check this :py:meth:`__init__ ` method: .. code-block:: python + def foo(s: str) -> str: return s @@ -96,8 +97,6 @@ flagged as an error. class C(): def __init__(self) -> None: # Must specify return type to type-check foo(1) # error: Argument 1 to "foo" has incompatible type "int"; expected "str" - - - **Some imports may be silently ignored**. Another source of unexpected ``Any`` values are the :option:`--ignore-missing-imports From a48a546b4c6746de4108becb551ac9a29cf98d44 Mon Sep 17 00:00:00 2001 From: Ivan Levkivskyi Date: Sat, 25 Jan 2020 13:12:20 +0000 Subject: [PATCH 3/3] Minor formatting tweak --- docs/source/common_issues.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/source/common_issues.rst b/docs/source/common_issues.rst index 0bfaeb1243bb..ed122b097005 100644 --- a/docs/source/common_issues.rst +++ b/docs/source/common_issues.rst @@ -75,10 +75,10 @@ flagged as an error. - **:py:meth:`__init__ ` method has no annotated arguments or return type annotation.** :py:meth:`__init__ ` is considered fully-annotated **if at least one argument is annotated**, - while mypy will infer the return type as 'None'. + while mypy will infer the return type as ``None``. The implication is that, for a :py:meth:`__init__ ` method that has no argument, you'll have to explicitly annotate the return type - as 'None' to type-check this :py:meth:`__init__ ` method: + as ``None`` to type-check this :py:meth:`__init__ ` method: .. code-block:: python