Skip to content

Commit

Permalink
Improve type inference when using decorators with pyright (#172)
Browse files Browse the repository at this point in the history
* Improve type inference when using decorators with pyright

* linting

---------

Co-authored-by: N. Ben Cohen <[email protected]>
Co-authored-by: Brian Pugh <[email protected]>
  • Loading branch information
3 people authored May 16, 2024
1 parent 1f5e3d6 commit c76368c
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions cyclopts/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
Literal,
Optional,
Tuple,
TypeVar,
Union,
)

Expand Down Expand Up @@ -51,6 +52,9 @@
to_tuple_converter,
)

T = TypeVar("T", bound=Callable)


if sys.version_info < (3, 9):
from typing_extensions import Annotated
else:
Expand Down Expand Up @@ -446,10 +450,10 @@ def parse_commands(self, tokens: Union[None, str, Iterable[str]] = None):

def command(
self,
obj: Optional[Callable] = None,
obj: Optional[T] = None,
name: Union[None, str, Iterable[str]] = None,
**kwargs,
) -> Callable:
) -> T:
"""Decorator to register a function as a CLI command.
Parameters
Expand All @@ -466,7 +470,7 @@ def command(
Any argument that :class:`App` can take.
"""
if obj is None: # Called ``@app.command(...)``
return partial(self.command, name=name, **kwargs)
return partial(self.command, name=name, **kwargs) # pyright: ignore[reportReturnType]

if isinstance(obj, App):
app = obj
Expand Down Expand Up @@ -506,18 +510,18 @@ def command(

app._parents.append(self)

return obj
return obj # pyright: ignore[reportReturnType]

def default(
self,
obj: Optional[Callable] = None,
obj: Optional[T] = None,
*,
converter=None,
validator=None,
):
) -> T:
"""Decorator to register a function as the default action handler."""
if obj is None: # Called ``@app.default_command(...)``
return partial(self.default, converter=converter, validator=validator)
return partial(self.default, converter=converter, validator=validator) # pyright: ignore[reportReturnType]

if isinstance(obj, App): # Registering a sub-App
raise TypeError("Cannot register a sub-App to default.")
Expand Down

0 comments on commit c76368c

Please sign in to comment.