What is the equivalent to Dart's ! operator in python (for typing)

Question:

I’m building a website with a Flutter/Dart front-end and a python backend. I absolutely love Dart’s rigorous static checking (saves me ridiculous amounts of time debugging), so I’ve started using type checking in python as well.

In Dart / Flutter, I can specify a variable as having type int or null as follows:

final int? myInt = 1; 

When working with myInt at future points in the code, I can then tell Dart’s type checker that the variable is currently not null by appending an exclamation mark, e.g. as follows:

final int myIntNotNull = myInt!; 

In python, I can similarly specify a variable as having type int or null as follows:

myInt:int|None = 1

My question is: Is there a similarly concise way to tell python’s type checker that I am sure that myInt is currently not null?

Asked By: Martin Reindl

||

Answers:

After some discussion and further research, it seems that the best option for this is to use the typing cast functionality.

from typing import cast
myIntNotNull:str = cast(int,myInt)
Answered By: Martin Reindl
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.