Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply Sourcery suggested refactoring. #448

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

Conversation

vlebourl
Copy link
Collaborator

@vlebourl vlebourl commented Apr 8, 2022

Not sure you want to appy that, so I'll make it a draft for now. But I think it's relevant!

@vlebourl vlebourl marked this pull request as draft April 8, 2022 08:21
@github-actions github-actions bot added the enhancement New feature or request label Apr 8, 2022
@@ -48,6 +48,7 @@
TooManyConcurrentRequestsException,
TooManyExecutionsException,
TooManyRequestsException,
UnknownException,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sourcery - Raise specific exception instead of genericException

self._states = [State(**state) for state in states]
else:
self._states = []
self._states = [State(**state) for state in states] if states else []
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sourcery - Replace if statement with if expression

@@ -119,7 +120,7 @@ def __init__(
self.gateways: list[Gateway] = []
self.event_listener_id: str | None = None

self.session = session if session else ClientSession()
self.session = session or ClientSession()
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sourcery - Simplify if expression by using or

Copy link
Collaborator

Choose a reason for hiding this comment

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

Indeed

@@ -186,7 +187,7 @@ async def somfy_tahoma_get_access_token(self) -> str:
"""
# Request access token
async with self.session.post(
SOMFY_API + "/oauth/oauth/v2/token",
f"{SOMFY_API}/oauth/oauth/v2/token",
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sourcery - Use f-string instead of string concatenation

@@ -230,7 +231,7 @@ async def refresh_token(self) -> None:
# &grant_type=refresh_token&refresh_token=REFRESH_TOKEN
# Request access token
async with self.session.post(
SOMFY_API + "/oauth/oauth/v2/token",
f"{SOMFY_API}/oauth/oauth/v2/token",
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sourcery - Use f-string instead of string concatenation

@@ -764,7 +752,7 @@ async def check_response(response: ClientResponse) -> None:
result = await response.text()
if "Server is down for maintenance" in result:
raise MaintenanceException("Server is down for maintenance") from error
raise Exception(
raise UnknownException(
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sourcery - Raise a specific error instead of the general Exception.

@@ -823,7 +811,7 @@ async def check_response(response: ClientResponse) -> None:
if "Not such token with UUID: " in message:
raise NotSuchTokenException(message)

raise Exception(message if message else result)
raise UnknownException(message or result)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sourcery - Raise a specific error instead of the general Exception.
Sourcery - Simplify if expression by using or

@@ -79,3 +79,7 @@ class SomfyBadCredentialsException(BadCredentialsException):

class SomfyServiceException(Exception):
pass


class UnknownException(Exception):
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sourcery - Raise a specific error instead of the general Exception.

@@ -19,7 +19,7 @@ def obfuscate_email(email: str | None) -> str:

def obfuscate_string(input: str) -> str:
"""Mask string"""
return re.sub(r"[a-zA-Z0-9_.-]*", "*", str(input))
return re.sub(r"[a-zA-Z0-9_.-]*", "*", input)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sourcery - Remove unnecessary casts to int, str, float or bool

@@ -298,7 +299,7 @@ async def cozytouch_login(self) -> str:

jwt = jwt.strip('"') # Remove surrounding quotes

return jwt
return str(jwt)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

mypy: pyoverkiz/client.py:302: error: Returning Any from function declared to return "str" [no-any-return]

Copy link
Owner

Choose a reason for hiding this comment

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

We should cast it if we know it is a string for sure.

Copy link
Owner

@iMicknl iMicknl left a comment

Choose a reason for hiding this comment

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

Nice! Let's see if we can implement a few of these changes / fixes :).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants