tortoise / tortoise/tortoise-orm

"TypeError: Cannot instantiate typing.Union" with "OneToOneNullableRelation"

Open
#1,205 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.6k
Forks
516
Avg merge
2d 21h
Merged PRs (30d)
9

Description

Describe the bug
We get a TypeError when the field OneToOneNullableRelation is used.

Traceback (most recent call last):
  File "/root/senran_dev/run.py", line 5, in <module>
    from bot import SenranBot
  File "/root/senran_dev/bot.py", line 10, in <module>
    import db
  File "/root/senran_dev/db/__init__.py", line 1, in <module>
    from .models import *
  File "/root/senran_dev/db/models.py", line 21, in <module>
    class User(BaseModel):
  File "/root/senran_dev/db/models.py", line 23, in User
    activated_guild = fields.OneToOneNullableRelation(model_name='models.Guild')
  File "/usr/lib/python3.8/typing.py", line 729, in __call__
    result = self.__origin__(*args, **kwargs)
  File "/usr/lib/python3.8/typing.py", line 339, in __call__
    raise TypeError(f"Cannot instantiate {self!r}")
TypeError: Cannot instantiate typing.Union

Process finished with exit code 1

To Reproduce
Create a model with a OneToOneNullableRelation relation

Expected behavior
The model and the field should work properly.

Additional context
Nothing else to add.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the OneToOneNullableRelation declaration shown in db/models.py and trace the corresponding fields implementation in Tortoise ORM. Reproduce the Python 3.8 TypeError with a model containing this relation; done means the model imports successfully and the field works without attempting to instantiate typing.Union.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.