Skip to content

Commit 115a3ba

Browse files
committed
Remove python 3.9 syntax and fix admonitions
1 parent fad68f4 commit 115a3ba

3 files changed

Lines changed: 8 additions & 8 deletions

File tree

docs/advanced/advanced-relationships/self-referential.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ To allow more fine-grained control over it, the `Relationship` constructor allow
1616

1717
Since SQLAlchemy relationships provide the [`remote_side`](https://docs.sqlalchemy.org/en/20/orm/relationship_api.html#sqlalchemy.orm.relationship.params.remote_side){.external-link target=_blank} parameter for just such an occasion, we can leverage that directly to construct the self-referential pattern with minimal code.
1818

19-
{* ./docs_src/advanced/self_referential/tutorial001_py39.py ln[6:18] hl[16] *}
19+
{* ./docs_src/advanced/self_referential/tutorial001_py310.py ln[6:16] hl[14] *}
2020

2121
Using the `sa_relationship_kwargs` parameter, we pass the keyword argument `remote_side='Villain.id'` to the underlying relationship property.
2222

23-
/// info
23+
/// note
2424

2525
The `remote_side` parameter accepts a Python-evaluable string when using Declarative. This allows us to reference `Villain.id` even though the class is still being defined.
2626

@@ -40,23 +40,23 @@ For our purposes, it is necessary that we also provide the `back_populates` para
4040

4141
In addition, the type annotations were made by enclosing our `Villain` class name in quotes, since we are referencing a class that is not yet fully defined by the time the interpreter reaches those lines. See the chapter on [type annotation strings](../../tutorial/relationship-attributes/type-annotation-strings.md){.internal-link target=_blank} for a detailed explanation.
4242

43-
Finally, as with regular (i.e. non-self-referential) foreign key relationships, it is up to us to decide whether it makes sense to allow the field to be **empty** or not. In our example, not every villain must have a boss (in fact, we would otherwise introduce a circular reference chain, which would not make sense in this context). Therefore we declare `boss_id: Optional[int]` and `boss: Optional['Villain']`. This is analogous to the `Hero``Team` relationship we saw [in an earlier chapter](../../tutorial/relationship-attributes/define-relationships-attributes.md#relationship-attributes-or-none){.internal-link target=_blank}.
43+
Finally, as with regular (i.e. non-self-referential) foreign key relationships, it is up to us to decide whether it makes sense to allow the field to be **empty** or not. In our example, not every villain must have a boss (in fact, we would otherwise introduce a circular reference chain, which would not make sense in this context). Therefore we declare `boss_id: int | None` and `boss: 'Villain' | None`. This is analogous to the `Hero``Team` relationship we saw [in an earlier chapter](../../tutorial/relationship-attributes/define-relationships-attributes.md#relationship-attributes-or-none){.internal-link target=_blank}.
4444

4545
## Creating instances
4646

4747
Now let's see how we can create villains with a boss:
4848

49-
{* ./docs_src/advanced/self_referential/tutorial001_py39.py ln[31:50] hl[34:35] *}
49+
{* ./docs_src/advanced/self_referential/tutorial001_py310.py ln[29:48] hl[32:33] *}
5050

5151
Just as with regular relationships, we can simply pass our boss villain as an argument to the constructor using `boss=thinnus`.
5252

5353
If we later learn that a villain actually had a secret boss after we've already created him, we can just as easily assign that boss retroactively:
5454

55-
{* ./docs_src/advanced/self_referential/tutorial001_py39.py ln[31:32,52:56] hl[52] *}
55+
{* ./docs_src/advanced/self_referential/tutorial001_py310.py ln[29:30,50:54] hl[50] *}
5656

5757
And if we want to add minions to a boss afterward, it's as easy as adding items to a Python list (because that's all it is 🤓):
5858

59-
{* ./docs_src/advanced/self_referential/tutorial001_py39.py ln[31:32,58:69] hl[61] *}
59+
{* ./docs_src/advanced/self_referential/tutorial001_py310.py ln[29:30,56:67] hl[59] *}
6060

6161
Since our relationships work both ways, we don't even need to add all our `clone_bot_`s to the session individually. Instead, we can simply add `ultra_bot` again and commit the changes. We do need to refresh them individually, though, if we want to access their updated attributes.
6262

@@ -71,7 +71,7 @@ top_boss_minions = clone_bot_3.boss.boss.minions
7171
assert any(minion is ebonite_mew for minion in top_boss_minions) # passes
7272
```
7373

74-
/// info
74+
/// note
7575

7676
Notice that we can, in fact, check for **identity** using `is` instead of `==` here, since we are dealing with the exact same objects, not just objects containing the same **data**.
7777

docs_src/advanced/self_referential/tutorial001_py39.py renamed to docs_src/advanced/self_referential/tutorial001_py310.py

File renamed without changes.

tests/test_advanced/test_self_referential/test_tutorial001.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181

8282

8383
def test_tutorial(clear_sqlmodel):
84-
from docs_src.advanced.self_referential import tutorial001_py39 as mod
84+
from docs_src.advanced.self_referential import tutorial001_py310 as mod
8585

8686
mod.sqlite_url = "sqlite://"
8787
mod.engine = create_engine(mod.sqlite_url)

0 commit comments

Comments
 (0)