Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have static type assertions in PyCharm?

Tags:

python

pycharm

def someproperty(self, value):
    """
    :type value: int
    """
    assert isinstance(value, int)
    # other stuff

I'd like Pycharm to assert when the user sets the value to something other than an int. I'm already using a type hint. Is there another way to get this functionality? Thanks in advance for any insight you can provide.

like image 404
laura Avatar asked Oct 20 '15 03:10

laura


People also ask

Can assertions be used for static type checking?

Static assertions are used to check if a condition is true when the code is compiled. If it isn't, the compiler is required to issue an error message and stop the compiling process. A static assertion is one that is checked at compile time, not run time.

Does Python have static type checking?

Python will always remain a dynamically typed language. However, PEP 484 introduced type hints, which make it possible to also do static type checking of Python code. Unlike how types work in most other statically typed languages, type hints by themselves don't cause Python to enforce types.

Does PyCharm have a type checker?

Type hints validation Any time you're applying type hints, PyCharm checks if the type is used correctly according to the supported PEPs. If there is a usage error, the corresponding warning is shown and the recommended action is suggested.

Are Python type hints enforced?

Type hints are performed using Python annotations (introduced since PEP 3107). They are used to add types to variables, parameters, function arguments as well as their return values, class attributes, and methods. Adding type hints has no runtime effect: these are only hints and are not enforced on their own.


2 Answers

Using pycharm you can get somewhat close to static type checking, using type declarations and increasing the severity of the "Type checker" inspection:

enter image description here

This will make type checks very prominent in your code:

enter image description here

like image 200
sebastian Avatar answered Oct 02 '22 17:10

sebastian


Python recently had a big development around static typing. Starting with Python 3.5, type hints are a thing. That’s what PEP 0484 was all about. The type hinting syntax is completely based on the function annotation syntax that was introduced way earlier with PEP 3107. This allowed PEP 0484 to be a change that did not involve new syntax which makes it very attractive and easy to adapt.

So, how does that work? The syntax is actually pretty intuitive for simple cases. For example:

def greeting(name: str) -> str:
    return 'Hello ' + name

The type hinting system does support very complicated things too though. You can also use user types, callables, multiple overloads using unions, even generic types. You can see a lot of examples in the PEP itself.

You can also watch Guido van Rossum’s talk about type hinting at the last PyCon, it’s really interesting and covers a lot of details.

Finally, with all these type hints, what do we do with them? Well, there is this great library that has been existing for a while, well before Python 3.5 and PEP 0484. Actually, the type hinting syntax officially introduced now is based on that library but just formalized. The library is called mypy and is basically a static type checker for Python. When installed, you can use the mypy executable to type check any Python script that contains type annotations.

For example, let’s put above function definition in a file, and call it with the wrong type arguments:

greeting(123)

Running mypy on the file, gives the following output:

$ mypy test.py
test.py:4: error: Argument 1 to "greeting" has incompatible type "int"; expected "str"

mypy, while technically experimental, is a very powerful tool that works really well. If you’re into this thing and would adopt type annotations, then you should really check it out.

like image 38
poke Avatar answered Oct 02 '22 17:10

poke