Skip to content

Commit

Permalink
add has_default and has_annotation properties
Browse files Browse the repository at this point in the history
  • Loading branch information
zunda-arrow committed Sep 8, 2022
1 parent f1dffd4 commit 14f5012
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions sigparse/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# MIT License

# Copyright (c) 2022 Lunarmagpie
# L37-L48 Copyright (c) 2022 Endercheif
# L37-L51 Copyright (c) 2022 Endercheif

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -70,20 +70,30 @@ def global_PEP604() -> None:
@dataclasses.dataclass
class Parameter:
"""
`default` is `inspect._empty` when there is no default.
`default` and `annotation` are `inspect._empty` when there is no default or
annotation respectively.
"""

name: str
annotation: typing.Any
default: typing.Any
kind: inspect._ParameterKind

@property
def has_default(self) -> bool:
"""
Return `True` if this argument has a default value.
"""
return not self.default is inspect._empty

@property
def has_annotation(self) -> bool:
"""
Return `True` if this argument has an annotation.
"""
return not self.annotation is inspect._empty



def _convert_signiture(
param: inspect.Parameter, type_hints: dict[str, type[typing.Any]]
Expand All @@ -97,9 +107,12 @@ def _convert_signiture(
)


def sigparse(func: typing.Callable[..., typing.Any]) -> typing.Sequence[Parameter]:
def sigparse(func: typing.Callable[..., typing.Any]) -> list[Parameter]:
if sys.version_info >= (3, 10):
return inspect.signature(func, eval_str=True).parameters.values()
return [
_convert_signiture(param, {}) for param in
inspect.signature(func, eval_str=True).parameters.values()
]

localns: dict[str, typing.Any] = {
"list": typing.List,
Expand Down

0 comments on commit 14f5012

Please sign in to comment.