Skip to content

Add stubs for assertpy - #11916

Merged
JelleZijlstra merged 4 commits into
python:mainfrom
rafaph:assertpy
May 15, 2024
Merged

Add stubs for assertpy#11916
JelleZijlstra merged 4 commits into
python:mainfrom
rafaph:assertpy

Conversation

@rafaph

@rafaph rafaph commented May 15, 2024

Copy link
Copy Markdown
Contributor

No description provided.

@github-actions

This comment has been minimized.

@rafaph

rafaph commented May 15, 2024

Copy link
Copy Markdown
Contributor Author

Hi dear maintainers!

I added here stubs for this amazing assertion library called assertpy. I have been using this library for a long time and it always bothers me to add ignore_missing_imports in the pyproject.toml file every time I start a new project.

So, I hope this makes someone besides me a little happier while using this library. 😄

@github-actions

This comment has been minimized.

@JelleZijlstra JelleZijlstra left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

If you have a lot of existing code using this library, I'd encourage you to typecheck that code with these stubs: it may help catch some bugs in the stubs.

Comment thread stubs/assertpy/assertpy/dynamic.pyi Outdated

__tracebackhide__: bool

Iterable: TypeAlias = _Iterable[Any]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a complicated way of importing Iterable, it doesn't need to be in the stub. (Also in some other files.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

Comment thread stubs/assertpy/assertpy/extracting.pyi Outdated
from typing_extensions import Self, TypeAlias

Iterable: TypeAlias = _Iterable[Any]
str_types: tuple[type[str]]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would omit this too, it's just Python 2/3 compatibility stuff.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I removed str_types, Iterable, and one xrange!

Comment thread stubs/assertpy/assertpy/dynamic.pyi Outdated
Iterable: TypeAlias = _Iterable[Any]

class DynamicMixin:
def __getattr__(self, attr: str) -> Self: ...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit unfortunate because if you use a typo on an attribute, you won't get an immediate error. Looking at the implementation, it only supports attributes starting with has_, but that can't be expressed in the type system.

However, it returns a callable, not a Self:

Suggested change
def __getattr__(self, attr: str) -> Self: ...
def __getattr__(self, attr: str) -> Callable[..., Self]: ...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I missed that, fixed!

@rafaph

rafaph commented May 15, 2024

Copy link
Copy Markdown
Contributor Author

Thanks!

If you have a lot of existing code using this library, I'd encourage you to typecheck that code with these stubs: it may help catch some bugs in the stubs.

Thanks, I had one minor problem with the exception.pyi and fixed it! :)

@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@JelleZijlstra
JelleZijlstra merged commit 5079dc1 into python:main May 15, 2024
Sign up for free to 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.

2 participants