JGFMK
JGFMK

Reputation: 8904

Python PEP 484 Type Hints -> return type either class name or None?

I'm using Python 3.6.5.

Class A, below for me represents a database table, using SQLAlchemy.

I'm defining a @staticmethod method that returns a row, but if there's no result, it would return None.

Since it returns an instance of class A, then the notation normally goes:

-> A:

at the end of the def signature, but because A is not yet defined, as it's on class A itself, you are supposed to quote it as:

-> 'A':

Is the -> 'A': sufficient? Or is there some sort of OR syntax?

Thanks in advance for your advice.

Upvotes: 1

Views: 229

Answers (1)

Gelineau
Gelineau

Reputation: 2090

You can use Optional[A], this means that it can return A or None

To make a "or" between classes A and B, use Union[A, B]

Note that you should import Optional and Union from typing

Upvotes: 1

Related Questions