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

fix(typing): use proper type for SinkCallable **kwargs #2411

Merged
merged 1 commit into from
Nov 20, 2024

Conversation

jap
Copy link
Contributor

@jap jap commented Nov 20, 2024

No description provided.

Copy link

codecov bot commented Nov 20, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (b541976) to head (c4af605).
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##            master     #2411   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           64        64           
  Lines         7726      7726           
  Branches      1071      1071           
=========================================
  Hits          7726      7726           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.


🚨 Try these New Features:

Copy link
Member

@CaselIT CaselIT left a comment

Choose a reason for hiding this comment

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

Hi,

The proposed typing is wrong. kwargs are not typed as dict.

Also when can you have none as the value?

@jap
Copy link
Contributor Author

jap commented Nov 20, 2024

Oh right, that kwargs thing not being a dict is one of those typing gotchas.

Anyway, to answer your question, kwargs is constructed using groupdict() which can return items with None when a regex matches but individual named groups don't, for example:

>>> re.match('(?P<foo>bar)?', 'baz').groupdict()
{'foo': None}

I'll give it another thought - what I'd like is being able to have my functions implement SinkCallable with a call signature

def my_sink(req: falcon.Request, resp, falcon.Response) -> None:
  ...

but mypy and other type checkers cannot infer that **kwargs is going to be empty in those cases so there will be a type mismatch.

I guess for now I'll have to write it as

def my_sink(req, resp, **_kwargs: object) -> None:
  assert not _kwargs, 'got unexpected extra arguments in my_sink'
  ...

which somewhat nullifies the elegance of passing these things as keyword arguments :(

Long term I can imagine having add_sink require an explicit string or regex as prefix (and no more implicit coercion to regex), so that it would be possible to have a typing-overload for add_sink that accepts a SinkCallable without kwargs if a string is passed as prefix. However, this would mean a breaking change for people passing regular expressions as strings to add_sink.

@CaselIT
Copy link
Member

CaselIT commented Nov 20, 2024

kwargs is constructed using groupdict() which can return items with None when a regex matches but individual named groups don't, for example:

understood!

regarding the call signature for sink, maybe we can use @overload so that both types are accepted?

Copy link
Member

@vytas7 vytas7 left a comment

Choose a reason for hiding this comment

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

Thanks for this improvement @jap, yes, the values may be None in the case of optional unmatched regex groups.

@vytas7 vytas7 merged commit 66eb886 into falconry:master Nov 20, 2024
31 checks passed
@CaselIT
Copy link
Member

CaselIT commented Nov 20, 2024

oh I wanted to try overload in this branch, but I guess we can do a follow on change for it

@vytas7
Copy link
Member

vytas7 commented Nov 20, 2024

Aha I thought you were planning a follow-up anyway since you approved 😅

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.

3 participants