How to get warnings for wrong return types in PyCharm?

Viewed 118

In a Python 3.7.1 project a method is defined with return type incompatible with the result type. However, the IDE, PyCharm 2018.2, gives no warnings. Here's a code example:

import numpy as np
from dataclasses import dataclass
import typing

# Definitions:
@dataclass(order=True)
class Theta:
  vector: np.ndarray

def a(self) -> typing.AnyStr:
    return self.vector.size

def b(self) -> str:
    return self.vector.size

# Client code:
x = np.array([1, 2])
s = Theta(x).a().capitalize() # runtime errors
u = Theta(x).b().capitalize()

Is there a way to enforce type warnings, both in the definitions and the client code?

Note: There're several questions about Python type hints on SO, e.g., Pycharm strange warning when type hinting. However, they seem to focus on a different aspect.

1 Answers

typing.AnyStr is a type variable. It means that it will be inferred from passed parameter or containing class. In your case neither class, nor method does not use this type variable and as a result inferred a return type is Any.

Related