diff --git a/docs/source/common_issues.rst b/docs/source/common_issues.rst index a4da8ec135465..b705717a0cdcb 100644 --- a/docs/source/common_issues.rst +++ b/docs/source/common_issues.rst @@ -339,6 +339,22 @@ style anyway). We can write the above code without a cast by using g(o + 1) # Okay; type of o is inferred as int here ... +Note that Mypy cannot always narrow a type based on ``isinstance()`` +used within a ternary/conditional expression, e.g.: + +.. code-block:: python + + def f(x: AnyStr) -> bytes: + r = x.encode('ascii') if isinstance(x, str) else x + # results in 'error: "bytes" has no attribute "encode"; maybe "decode"?' + + if isinstance(x, str): + r = x.encode('ascii') # whereas Mypy can narrow within an if statement + else: + r = x + return r + ... + Type inference in mypy is designed to work well in common cases, to be predictable and to let the type checker give useful error messages. More powerful type inference strategies often have complex