Skip to content
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

Replace ClientConstructor with a TypedDict #2820

Open
szokeasaurusrex opened this issue Mar 13, 2024 · 0 comments
Open

Replace ClientConstructor with a TypedDict #2820

szokeasaurusrex opened this issue Mar 13, 2024 · 0 comments

Comments

@szokeasaurusrex
Copy link
Member

szokeasaurusrex commented Mar 13, 2024

We currently use ClientConstructor to trick mypy (and other static analyzers) into believing that Client.__init__ takes many typed parameters, rather than untyped **kwargs. The current solution is somewhat hacky, as it separates the Client class's actual implementation into a separate _Client class.

We could instead define a TypedDict with the parameters accepted in Client.__init__'s **kwargs, and type the **kwargs with this TypedDict. I have already implemented similar functionality for start_transaction in #2796.

Advantages of switching to a TypedDict include:

  • Code is clearer, since we are not fooling mypy with hacky workarounds.
  • mypy enforces the **kwargs's types within Client.__init__. Currently, **kwargs is recognized as a dict[str, Any], which is type-unsafe.
  • mypy currently incorrectly recognizes the arguments in ClientConstructor as all being valid positional arguments, when in reality, only the dsn may be provided as a positional argument. Providing any other arguments as positional arguments results in a TypeError. With a TypedDict, mypy is aware that the arguments must be passed as keyword arguments.

Disadvantages of switching to a TypedDict:

  • The current solution allows us to indicate the default value's of the arguments, when they are not provided. Specifying default values may not be possible with a TypedDict.
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

No branches or pull requests

1 participant