-
Notifications
You must be signed in to change notification settings - Fork 992
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lint: use type narrowing for
orm.object_session()
(#17582)
Since `object_session(...)` can return `None`, we need to help mypy have confidence that the session is indeed there. One way to narrow the type is to assert that it's not `None`, and therefore should not alert about `union-attr` problems. Includes a couple of renames, and variable extraction where relevant. Signed-off-by: Mike Fiedler <miketheman@gmail.com>
- Loading branch information
1 parent
1a5b4fe
commit 74b7a87
Showing
9 changed files
with
96 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import pytest | ||
|
||
from sqlalchemy.orm import object_session | ||
|
||
from warehouse.db import Model | ||
from warehouse.utils.db.orm import NoSessionError, orm_session_from_obj | ||
|
||
|
||
def test_orm_session_from_obj_raises_with_no_session(): | ||
|
||
class FakeObject(Model): | ||
__tablename__ = "fake_object" | ||
|
||
obj = FakeObject() | ||
# Confirm that the object does not have a session with the built-in | ||
assert object_session(obj) is None | ||
|
||
with pytest.raises(NoSessionError): | ||
orm_session_from_obj(obj) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""ORM utilities.""" | ||
|
||
from sqlalchemy.orm import Session, object_session | ||
|
||
|
||
class NoSessionError(Exception): | ||
"""Raised when there is no active SQLAlchemy session""" | ||
|
||
|
||
def orm_session_from_obj(obj) -> Session: | ||
""" | ||
Returns the session from the ORM object. | ||
Adds guard, but it should never happen. | ||
The guard helps with type hinting, as the object_session function | ||
returns Optional[Session] type. | ||
""" | ||
session = object_session(obj) | ||
if not session: | ||
raise NoSessionError("Object does not have a session") | ||
return session |