Skip to content

Type | None not working when Type is a class #923

Answered by joachimhuet
darhsu asked this question in Questions
Discussion options

You must be logged in to vote

Mmh in fact I don't think it's a bug. It seems normal to not be able to use an object that has not yet been defined. By doing this if TYPE_CHECKING: ... we just import during type checking, meaning that we do not have access to the object yet. And probably, in Relationship there is a use of that.

And in fact for the example I gave using pure pydantic if you import Hero and instantiate Hero before importing Team you should have an error. This is also the case with the example from the documentation using Optional[Team] if you only import hero and try to instantiate a hero without giving it a team you will have the same KeyError: 'Team'. And having to keep in mind that a Team must be import…

Replies: 2 comments 5 replies

Comment options

You must be logged in to vote
1 reply
@darhsu
Comment options

Comment options

You must be logged in to vote
4 replies
@darhsu
Comment options

@joachimhuet
Comment options

@darhsu
Comment options

@joachimhuet
Comment options

Answer selected by darhsu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
3 participants