Skip to content

Clean the code, type annotations and update dependencies #143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 15 commits into from

Conversation

arnaud-ma
Copy link
Contributor

@arnaud-ma arnaud-ma commented Jul 7, 2024

Some code must have been added before Python 3.8. We can put this version to good use. These fixes do not modify the runtime in any way.

Changes:

  • 2 or 3 typos
  • No need to use inspect_typing, there is built-in functions in the typing module to replace them (exactly the same code)
  • More precise type hints: use of typing.Protocol and typing.TypedDict instead of bare dict, callable or types
  • Remove old code only used in Python < 3.8

I think it is possible to go much further in type annotation (e.g. by annotating some methods with *args and **kwargs). But for this, typing-extensions must be added as a dependency.

I thought for a long time that using from __future__ import annotations to use dict[str | int, int] | List[str |int] instead of Union[Dict[Union[str, int], int], List[Union[str, int]] would be a good idea, but the package itself does not support this syntax so it would only be an additional source of bugs.

tap/utils.py Outdated
Comment on lines 34 to 41
def is_literal_type(tp: type) -> bool:
"""Returns whether the type is a literal type."""
return tp is Literal or getattr(tp, "__origin__", None) is Literal

def get_origin(tp: type) -> type:
"""Same as typing.get_origin but returns the type itself if the origin is None."""
origin = typing_get_origin(tp)
return origin if origin is not None else tp
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The pieces of code that must be added to remove typing_inspect from dependencies. There is little risk of having to modify it for future versions of python since almost everything is done in the typing module

@arnaud-ma arnaud-ma closed this Jul 11, 2024
@martinjm97
Copy link
Collaborator

What's the story here?

@arnaud-ma
Copy link
Contributor Author

I apologize for the waste of time. I realized there were too many conflicts with other pull requests. It seems easier to me to make a (much) cleaner one later. The goals remain: update the < 3.8 Python code and add more precise type hints.

@martinjm97
Copy link
Collaborator

I see. Sorry for the conflicts! I think making a few smaller PRs will make it easier to merge! Thanks!

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