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

Add invoice_payload filtering #4005

Merged

Conversation

aelkheir
Copy link
Contributor

@aelkheir aelkheir commented Dec 12, 2023

Closes #3752

  • Added .. versionadded:: NEXT.VERSION, .. versionchanged:: NEXT.VERSION or .. deprecated:: NEXT.VERSION to the docstrings for user facing changes (for methods/class descriptions, arguments and attributes)
  • Created new or adapted existing unit tests
  • Documented code changes according to the CSI standard <https://standards.mousepawmedia.com/en/stable/csi.html>__
  • Added myself alphabetically to AUTHORS.rst (optional)
  • Added new classes & modules to the docs and all suitable __all__ s
  • Checked the Stability Policy <https://docs.python-telegram-bot.org/stability_policy.html>_ in case of deprecations or changes to documented behavior

why not add collect_additional_context in PreCheckoutQueryHandler to store the match since we're adding a pattern argument. i can do it if you approve.

Add class `filters.SuccessfulPayment` with support for `invoice_payload`
filtering.
@Bibo-Joshi
Copy link
Member

Thanks for the PR! I have two general comments that I'd like you to address before I review in details:

@aelkheir
Copy link
Contributor Author

Thanks for the feedback.

This was added to CallbackQueryHandler only for usage with arbitrary callback_data

Aha got it, I did not understand why it was there

I would even argue that we don't need to allow callables.

Ok. I thought it was needed because of this #3773 (comment)

Please add unit tests for the code changes and ensure that the existing ones don't fail.

On it.

@aelkheir
Copy link
Contributor Author

aelkheir commented Dec 12, 2023

I created some tests. I see some checks are still failing. Doc build is failing, although it built fine locally.

@aelkheir aelkheir changed the title invoice_payload filtering in (new) filters.SuccessfulPayment and (existing) PreCheckoutQueryHandler Add invoice_payload filtering Dec 14, 2023
@aelkheir
Copy link
Contributor Author

Couldn't figure out why pytest (3.8, macos-latest) is still failing. But I think its ready for review. @Bibo-Joshi

Copy link
Member

@Bibo-Joshi Bibo-Joshi 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 the nice PR! I left a few smaller comments below :) the tests on macOS recently tend to fail due to flakyness in time-based jobs …

def __init__(
self,
callback: HandlerCallback[Update, CCT, RT],
pattern: Optional[Union[str, Pattern[str]]] = None,
Copy link
Member

Choose a reason for hiding this comment

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

please move the argument to the end of the list. Otherwise this would be a breaking change as PCQH(callback, False) would no longer work as expected. Please also move the docstring entries for argument and attribute above.

Comment on lines 83 to 86
if isinstance(pattern, str):
pattern = re.compile(pattern)

self.pattern: Optional[Union[str, Pattern[str]]] = pattern
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if isinstance(pattern, str):
pattern = re.compile(pattern)
self.pattern: Optional[Union[str, Pattern[str]]] = pattern
self.pattern: Optional[Pattern[str]] = re.compile(pattern) if pattern is not None else None

Just a bit shorter and also gives the attribute a more narrow type.
This way we can also use self.pattern.match(…) in check_update below :)

tests/ext/test_filters.py Show resolved Hide resolved
@@ -103,6 +106,14 @@ async def callback(self, update, context):
and isinstance(update.pre_checkout_query, PreCheckoutQuery)
)

def test_with_pattern(self, pre_checkout_query):
handler = PreCheckoutQueryHandler(self.callback, pattern=".*voice.*")
Copy link
Member

Choose a reason for hiding this comment

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

please also test passing a compiled pattern :)

@aelkheir
Copy link
Contributor Author

Applied changes, ready for more

Copy link
Member

@Bibo-Joshi Bibo-Joshi 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 the updates and sorry for the late review. LGTM now :) Let me check if one of the other team members can also have a second look.

block: DVType[bool] = DEFAULT_TRUE,
pattern: Optional[Pattern[str]] = None,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
pattern: Optional[Pattern[str]] = None,
pattern: Optional[Union[str, Pattern[str]]] = None,

accepting a string as input is okay, my point in #4005 (comment) was just that we can convert strings to patterns on init already :) sorry if that was unclear.

tests/ext/test_filters.py Show resolved Hide resolved
tests/ext/test_precheckoutqueryhandler.py Show resolved Hide resolved
@harshil21 harshil21 self-requested a review December 30, 2023 02:24
@harshil21 harshil21 added this to the v20.8 milestone Dec 30, 2023
Copy link
Member

@Poolitzer Poolitzer left a comment

Choose a reason for hiding this comment

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

LGTM

@Bibo-Joshi Bibo-Joshi merged commit f3479cd into python-telegram-bot:master Jan 2, 2024
22 of 23 checks passed
@Bibo-Joshi
Copy link
Member

Thanks very much for the nice contribution :)

@aelkheir
Copy link
Contributor Author

aelkheir commented Jan 3, 2024

Ha! No really, thank you. I learnt something new.

@aelkheir aelkheir deleted the invoice-payload-filtering branch January 10, 2024 21:27
@github-actions github-actions bot locked and limited conversation to collaborators Jan 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Filter support for invoice_payload in PreCheckoutQueryHandler and successful_payment message
4 participants