Skip to content

Decrator utils - #95

Open
dev0Guy wants to merge 5 commits into
PythonNest:mainfrom
dev0Guy:dev-decrator-utils
Open

Decrator utils#95
dev0Guy wants to merge 5 commits into
PythonNest:mainfrom
dev0Guy:dev-decrator-utils

Conversation

@dev0Guy

@dev0Guydev0Guy commented Dec 29, 2024

Copy link
Copy Markdown
Contributor

User description

rewrite some of the decrator utils.
By converting for loops into list, dict comprehension , I've a lot of wrapped try, exception section inside the loop with re-raise, I've removed them (there is no unique logic inside the exception block).

Original

defget_non_dependencies_params(cls):
source=inspect.getsource(cls.__init__).strip()
tree=ast.parse(source)
non_dependencies= {}
fornodeinast.walk(tree):
ifisinstance(node, ast.Attribute):
non_dependencies[node.attr] =node.value.idreturnnon_dependencies

Changed

defget_non_dependencies_params(cls: Type):
source=inspect.getsource(cls.__init__).strip()
tree=ast.parse(source)
return {
node.attr: node.value.idfornodeinast.walk(tree)
ifisinstance(node, ast.Attribute)
}

Forthermore, I've seen duplicate code inside get_instance_variables where the dependencies is already written inside parse_dependencies.

dependencies=set(
param.nameforparamininspect.signature(cls.__init__).parameters.values()
ifparam.annotation!=param.emptyandgetattr(param.annotation, "__injectable__", False)
)
defparse_dependencies(cls):
signature=inspect.signature(cls.__init__)
dependecies= {}
forparaminsignature.parameters.values():
try:
if (
param.annotation!=param.emptyandhasattr(param.annotation, "__dict__")
andINJECTABLE_TOKENinparam.annotation.__dict__
):
dependecies[param.name] =param.annotationexceptExceptionase:
raiseereturndependecies

I didn't understand why the need to check only object ( not including inherited attributes) however I assume a usage and created another argument for parse_dependencies to decide which check to activate.
and got:

defparse_dependencies(cls: Type, check_inherited: bool=False) ->Dict[str, Type]:
""" Returns: mapping of injectable parameters name to there annotation """signature=inspect.signature(cls.__init__)
filter_by= (
_check_injectable_inheritedifcheck_inheritedelse_check_injectable_not_inherited
)
params: Iterable[inspect.Parameter] =filter(filter_by, signature.parameters.values())
return {param.name: param.annotationforparaminparams}

Generated description

Below is a concise technical summary of the changes proposed in this PR:

Refactors and optimizes utility functions in the nest/core/decorators/utils.py file. Converts for loops into list and dictionary comprehensions, removes unnecessary try-except blocks, and introduces type hints. Adds new helper functions to improve code readability and maintainability. Modifies the parse_dependencies function to handle both inherited and non-inherited injectable attributes.

TopicDetails
Code OptimizationRefactors utility functions to improve performance and readability
Modified files (1)
  • nest/core/decorators/utils.py
Latest Contributors(2)
UserCommitDate
itay.dar@lemonade.comfeat-build-cli-apps-wi...August 06, 2024
amirm.lavasani@gmail.comAdd-HTTP-Status-Code-F...June 24, 2024
Dependency HandlingEnhances dependency parsing with options for inherited and non-inherited attributes
Modified files (1)
  • nest/core/decorators/utils.py
Latest Contributors(2)
UserCommitDate
itay.dar@lemonade.comfeat-build-cli-apps-wi...August 06, 2024
amirm.lavasani@gmail.comAdd-HTTP-Status-Code-F...June 24, 2024
This pull request is reviewed by Baz. Join @dev0Guy and the rest of your team on (Baz).

Sign up for freeto 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

@dev0Guy