Skip to content

Commit

Permalink
re-write to comfort pylint (Codacy)
Browse files Browse the repository at this point in the history
  • Loading branch information
markus-96 committed Jan 28, 2025
1 parent 4e96190 commit a54fcb8
Showing 1 changed file with 12 additions and 20 deletions.
32 changes: 12 additions & 20 deletions tortoise/fields/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,26 +347,18 @@ def get_for_dialect(self, dialect: str, key: str) -> Any:
:param dialect: The requested SQL Dialect.
:param key: The attribute/method name.
"""
dialect_cls = getattr(self, f"_db_{dialect}", None) # get, if present, the dialect class
# get, if present, the key of the dialect class. If no dialect class was found previously, it will also be None:
dialect_value = getattr(dialect_cls, key, None)
if dialect_value is None: # if the key was not found in the dialect class, we have to look in self
v = getattr(self, key, None) # get the requested key if present, also works with property
return v # and return it
# it could be that dialect_value is a computed property, like in CharField._db_oracle.SQL_TYPE,
# and therefore first need to instantiate dialect_cls
elif isinstance(dialect_value, property) and dialect_cls is not None:
try:
# instantiate the dialect_cls. Codacy does not like dialect_cls(self), so lets do it manually
dialect_cls_instance = dialect_cls.__new__(dialect_cls)
dialect_cls_instance.__init__(self)
except TypeError:
pass
else:
# and get the value from the property
return getattr(dialect_cls_instance, key)
return None
return dialect_value
try:
dialect_cls = getattr(self, f"_db_{dialect}") # throws AttributeError if not present
dialect_value = getattr(dialect_cls, key) # throws AttributeError if not present
except AttributeError:
pass
else: # we have dialect_cls and dialect_value, so lets use it
# it could be that dialect_value is a computed property, like in CharField._db_oracle.SQL_TYPE,
# and therefore one first needs to instantiate dialect_cls
if isinstance(dialect_value, property):
return getattr(dialect_cls(self), key)
return dialect_value
return getattr(self, key, None) # there is nothing special defined, return the value of self

def describe(self, serializable: bool) -> dict:
"""
Expand Down

0 comments on commit a54fcb8

Please sign in to comment.