Skip to content

Commit

Permalink
fix(function_analyzer): identify requried arguments based on defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
felixocker committed Jul 2, 2024
1 parent fc3a875 commit 1429e38
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/function_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,27 @@ def analyze_function(self, function_: Callable) -> dict:
f"Function '{function_name}' has arguments '{arguments}' but type hints only for '{type_hints}'."
)

# Get non-optional arguments.
required_arguments = [
# Get well-defined arguments.
well_defined_arguments = [
argument
for argument, type_ in type_hints.items()
if not (
typing.get_origin(type_) is Union
and type(None) in typing.get_args(type_)
)
]
# Get required arguments.
signature = inspect.signature(function_)
required_arguments = [
arg
for arg in well_defined_arguments
if signature.parameters.get(arg).default is inspect.Parameter.empty
]
# Convert type hints to basic types.
type_hints_basic = {
argument: (
type_
if argument in required_arguments
if argument in well_defined_arguments
else [t for t in typing.get_args(type_) if t][0]
)
for argument, type_ in type_hints.items()
Expand Down

0 comments on commit 1429e38

Please sign in to comment.