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.
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.
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.
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.
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.
Using pycharm you can get somewhat close to static type checking, using type declarations and increasing the severity of the "Type checker" inspection:
This will make type checks very prominent in your code:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With