#๐Ÿ”’ non-None assertion operator?

22 messages ยท Page 1 of 1 (latest)

kindred pier
#

Im using python3.10 with type checking/hints - and sometimes the type checker fails to determine that the type is not None because of an earlier/complicated existance check.

Is there a syntax equivalent to typescripts non-null-assertion operator? eg doSomething(i_am_not_null!)

wind fogBOT
#

@kindred pier

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

snow temple
#

assert x is not None?

gleaming mist
#

(That's a statement tho)

snow temple
#

@kindred pier

#

Ooh, yeah

#

Umm

kindred pier
#

I know I can do a cast, but its so messy

gleaming mist
#

you can probably write a function for it

snow temple
#

You could always make your own typegaurd function if you really need to do a expression

kindred pier
#
from typing import cast

doSomething(cast(MyType, my_non_null_type))
gleaming mist
#

I was thinking put the assert in a function

snow temple
#

Idk if that would work with a checker though

kindred pier
#

we've already verified the type isn't None elsewhere

#

its just the type checker that fails to reaize it

gleaming mist
#

the assert function would basically be a cast

#

But you would not specify the type you want it to be which is easier

kindred pier
#

hmmm

snow temple
#

And if i_am_not_null is a function call or something, just assign it to a variable first

wind fogBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.