Skip to content

Conversation

@sourcery-ai
Copy link

@sourcery-ai sourcery-ai bot commented Jun 24, 2022

Branch main refactored by Sourcery.

If you're happy with these changes, merge this Pull Request using the Squash and merge strategy.

See our documentation here.

Run Sourcery locally

Reduce the feedback loop during development by using the Sourcery editor plugin:

Review changes via command line

To manually merge these changes, make sure you're on the main branch, then run:

git fetch origin sourcery/main
git merge --ff-only FETCH_HEAD
git reset HEAD^

Help us improve this pull request!

@sourcery-ai sourcery-ai bot requested a review from synodriver June 24, 2022 07:27
Comment on lines -45 to +53
use_pr: Optional[PullRequest] = None
for pr in repo.get_pulls():
if pr.head.sha == event.workflow_run.head_commit.id:
use_pr = pr
break
use_pr: Optional[PullRequest] = next(
(
pr
for pr in repo.get_pulls()
if pr.head.sha == event.workflow_run.head_commit.id
),
None,
)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 45-49 refactored with the following changes:

  • Use the built-in function next instead of a for-loop (use-next)

Comment on lines -71 to +83
notified = False
for pr_comment in pr_comments:
if message in pr_comment.body:
notified = True
notified = any(message in pr_comment.body for pr_comment in pr_comments)
logging.info(f"Docs preview was notified: {notified}")
if not notified:
artifact_name = f"docs-zip-{commit}"
use_artifact: Optional[Artifact] = None
for artifact in artifacts_response.artifacts:
if artifact.name == artifact_name:
use_artifact = artifact
break
use_artifact: Optional[Artifact] = next(
(
artifact
for artifact in artifacts_response.artifacts
if artifact.name == artifact_name
),
None,
)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 71-82 refactored with the following changes:

  • Use any() instead of for loop (use-any)
  • Use the built-in function next instead of a for-loop (use-next)

):
heroes = session.exec(select(Hero).offset(offset).limit(limit)).all()
return heroes
return session.exec(select(Hero).offset(offset).limit(limit)).all()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function read_heroes refactored with the following changes:

with Session(engine) as session:
heroes = session.exec(select(Hero).offset(offset).limit(limit)).all()
return heroes
return session.exec(select(Hero).offset(offset).limit(limit)).all()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function read_heroes refactored with the following changes:

with Session(engine) as session:
heroes = session.exec(select(Hero).offset(offset).limit(limit)).all()
return heroes
return session.exec(select(Hero).offset(offset).limit(limit)).all()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function read_heroes refactored with the following changes:

with Session(engine) as session:
heroes = session.exec(select(Hero).offset(offset).limit(limit)).all()
return heroes
return session.exec(select(Hero).offset(offset).limit(limit)).all()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function read_heroes refactored with the following changes:

Comment on lines -201 to -208
relationship_info = RelationshipInfo(
return RelationshipInfo(
back_populates=back_populates,
link_model=link_model,
sa_relationship=sa_relationship,
sa_relationship_args=sa_relationship_args,
sa_relationship_kwargs=sa_relationship_kwargs,
)
return relationship_info
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Relationship refactored with the following changes:

Comment on lines -317 to +316
def __init__(
cls, classname: str, bases: Tuple[type, ...], dict_: Dict[str, Any], **kw: Any
) -> None:
def __init__(self, classname: str, bases: Tuple[type, ...], dict_: Dict[str, Any], **kw: Any) -> None:
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function SQLModelMetaclass.__init__ refactored with the following changes:

Comment on lines -434 to +431
foreign_key = getattr(field.field_info, "foreign_key", None)
if foreign_key:
if foreign_key := getattr(field.field_info, "foreign_key", None):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_column_from_field refactored with the following changes:

Comment on lines -522 to +521
if getattr(self.__config__, "table", False):
if is_instrumented(self, name):
set_attribute(self, name, value)
if getattr(self.__config__, "table", False) and is_instrumented(
self, name
):
set_attribute(self, name, value)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function SQLModel.__setattr__ refactored with the following changes:

@sourcery-ai
Copy link
Author

sourcery-ai bot commented Jun 24, 2022

Sourcery Code Quality Report

✅  Merging this PR will increase code quality in the affected files by 0.80%.

Quality metrics Before After Change
Complexity 6.66 ⭐ 5.72 ⭐ -0.94 👍
Method Length 47.65 ⭐ 47.03 ⭐ -0.62 👍
Working memory 9.18 🙂 9.20 🙂 0.02 👎
Quality 68.94% 🙂 69.74% 🙂 0.80% 👍
Other metrics Before After Change
Lines 2101 2064 -37
Changed files Quality Before Quality After Quality Change
.github/actions/comment-docs-preview-in-pr/app/main.py 38.03% 😞 42.69% 😞 4.66% 👍
.github/actions/watch-previews/app/main.py 45.66% 😞 54.18% 🙂 8.52% 👍
docs_src/tutorial/fastapi/app_testing/tutorial001/main.py 81.77% ⭐ 81.89% ⭐ 0.12% 👍
docs_src/tutorial/fastapi/delete/tutorial001.py 82.43% ⭐ 82.51% ⭐ 0.08% 👍
docs_src/tutorial/fastapi/limit_and_offset/tutorial001.py 87.34% ⭐ 87.61% ⭐ 0.27% 👍
docs_src/tutorial/fastapi/multiple_models/tutorial001.py 83.16% ⭐ 82.88% ⭐ -0.28% 👎
docs_src/tutorial/fastapi/multiple_models/tutorial002.py 88.50% ⭐ 88.36% ⭐ -0.14% 👎
docs_src/tutorial/fastapi/read_one/tutorial001.py 89.03% ⭐ 88.93% ⭐ -0.10% 👎
docs_src/tutorial/fastapi/relationships/tutorial001.py 74.66% 🙂 74.66% 🙂 0.00%
docs_src/tutorial/fastapi/response_model/tutorial001.py 92.15% ⭐ 92.13% ⭐ -0.02% 👎
docs_src/tutorial/fastapi/session_with_dependency/tutorial001.py 81.77% ⭐ 81.89% ⭐ 0.12% 👍
docs_src/tutorial/fastapi/simple_hero_api/tutorial001.py 93.07% ⭐ 93.41% ⭐ 0.34% 👍
docs_src/tutorial/fastapi/teams/tutorial001.py 76.70% ⭐ 76.73% ⭐ 0.03% 👍
docs_src/tutorial/fastapi/update/tutorial001.py 81.58% ⭐ 81.66% ⭐ 0.08% 👍
sqlmodel/main.py 56.87% 🙂 56.60% 🙂 -0.27% 👎
sqlmodel/engine/create.py 23.35% ⛔ 23.36% ⛔ 0.01% 👍
sqlmodel/sql/sqltypes.py 83.15% ⭐ 85.33% ⭐ 2.18% 👍
tests/conftest.py 78.64% ⭐ 84.70% ⭐ 6.06% 👍
tests/test_default.py 82.30% ⭐ 82.59% ⭐ 0.29% 👍
tests/test_tutorial/test_where/test_tutorial003.py 83.39% ⭐ 83.82% ⭐ 0.43% 👍
tests/test_tutorial/test_where/test_tutorial004.py 82.00% ⭐ 82.41% ⭐ 0.41% 👍
tests/test_tutorial/test_where/test_tutorial011.py 82.00% ⭐ 82.41% ⭐ 0.41% 👍

Here are some functions in these files that still need a tune-up:

File Function Complexity Length Working Memory Quality Recommendation
sqlmodel/engine/create.py create_engine 28 😞 443 ⛔ 36 ⛔ 11.68% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
sqlmodel/main.py SQLModelMetaclass.__init__ 33 ⛔ 213 ⛔ 16 ⛔ 23.23% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
sqlmodel/main.py SQLModelMetaclass.__new__ 17 🙂 257 ⛔ 14 😞 32.65% 😞 Try splitting into smaller methods. Extract out complex expressions
sqlmodel/main.py Field 1 ⭐ 125 😞 31 ⛔ 49.11% 😞 Try splitting into smaller methods. Extract out complex expressions
sqlmodel/main.py get_column_from_field 11 🙂 186 😞 9 🙂 50.96% 🙂 Try splitting into smaller methods

Legend and Explanation

The emojis denote the absolute quality of the code:

  • ⭐ excellent
  • 🙂 good
  • 😞 poor
  • ⛔ very poor

The 👍 and 👎 indicate whether the quality has improved or gotten worse with this pull request.


Please see our documentation here for details on how these metrics are calculated.

We are actively working on this report - lots more documentation and extra metrics to come!

Help us improve this quality report!

Comment on lines -544 to +541
if not getattr(cls.__config__, "table", False):
# If not table, normal Pydantic code
m: _TSQLModel = cls.__new__(cls)
else:
# If table, create the new instance normally to make SQLAlchemy create
# the _sa_instance_state attribute
m = cls()
m = cls() if getattr(cls.__config__, "table", False) else cls.__new__(cls)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function SQLModel.from_orm refactored with the following changes:

This removes the following comments ( why? ):

# the _sa_instance_state attribute
# If table, create the new instance normally to make SQLAlchemy create
# If not table, normal Pydantic code

Comment on lines -614 to +605
if include is None and exclude is None and exclude_unset is False:
if include is None and exclude is None and not exclude_unset:
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function SQLModel._calculate_keys refactored with the following changes:

This removes the following comments ( why? ):

# Updated to not return SQLAlchemy attributes
# recursion, or traversing the whole database
# Original in Pydantic:
# | self.__sqlmodel_relationships__.keys()
# keys = self.__dict__.keys()
# Do not include relationships as that would easily lead to infinite

Comment on lines -645 to +628
def __tablename__(cls) -> str:
return cls.__name__.lower()
def __tablename__(self) -> str:
return self.__name__.lower()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function SQLModel.__tablename__ refactored with the following changes:

if not isinstance(query_cache_size, _DefaultPlaceholder):
current_kwargs["query_cache_size"] = query_cache_size
current_kwargs.update(kwargs)
current_kwargs |= kwargs
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function create_engine refactored with the following changes:

Comment on lines -49 to +53
if not isinstance(value, uuid.UUID):
return f"{uuid.UUID(value).int:x}"
else:
# hexstring
return f"{value.int:x}"
return (
f"{value.int:x}"
if isinstance(value, uuid.UUID)
else f"{uuid.UUID(value).int:x}"
)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function GUID.process_bind_param refactored with the following changes:

This removes the following comments ( why? ):

# hexstring

value2 = Default("bar")

assert not (value1 == value2)
assert value1 != value2
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function test_not_equality refactored with the following changes:

  • Simplify logical expression using De Morgan identities (de-morgan)

value2 = "foo"

assert not (value1 == value2)
assert value1 != "foo"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function test_not_equality_other refactored with the following changes:

  • Inline variable that is only used once (inline-variable)
  • Simplify logical expression using De Morgan identities (de-morgan)

# Now that this item was checked, remove it from the list
calls.pop(calls.index(call))
assert len(calls) == 0, "The list should only have the expected items"
assert not calls, "The list should only have the expected items"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function test_tutorial refactored with the following changes:

# Now that this item was checked, remove it from the list
calls.pop(calls.index(call))
assert len(calls) == 0, "The list should only have the expected items"
assert not calls, "The list should only have the expected items"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function test_tutorial refactored with the following changes:

# Now that this item was checked, remove it from the list
calls.pop(calls.index(call))
assert len(calls) == 0, "The list should only have the expected items"
assert not calls, "The list should only have the expected items"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function test_tutorial refactored with the following changes:

synodriver pushed a commit that referenced this pull request Jun 24, 2022
perf: raise exception if no matching sqlalchemy type
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant