Skip to content

PoC: Bot instance inside method shortcuts using pydantic Validation Context #1210

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

Merged
merged 7 commits into from
Jul 11, 2023

Conversation

JrooTJunior
Copy link
Member

@JrooTJunior JrooTJunior commented Jul 8, 2023

Description

Proof of concept for a replacement of ContextVar's for Bot instance inside method shortcuts
with a implementation based on a new pydantic feature named Validation Context.

Motivation

The motivation behind this pull request is to enhance the current implementation of ContextVar's for the Bot instance inside method shortcuts. The existing approach using ContextVar's can be confusing for new users and lacks clarity in its usage. By leveraging a new feature called "Validation Context" in Pydantic, we can provide a more transparent and intuitive solution.

The Validation Context feature in Pydantic offers a clearer and more explicit way of passing the Bot instance to method shortcuts. It eliminates the ambiguity associated with ContextVar's and provides a straightforward mechanism for accessing and utilizing the Bot instance within the methods. Additionally, this new approach opens up possibilities for using the Validation Context for other purposes as well, enhancing its versatility and making it a valuable addition to the codebase.

Overall, by adopting the Validation Context-based implementation, we can improve the readability, understandability, and maintainability of the codebase while also providing a more intuitive way of handling the Bot instance within method shortcuts.

Type of change

Please delete options that are not relevant.

  • Documentation (typos, code examples or any documentation update)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Currently there are no tests for this feature.

I have tested this feature manually by running the example bot and checking if the bot instance is
correctly passed to the method shortcuts.

This feature should be discussed with the community before any tests are written.

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

@JrooTJunior JrooTJunior added under discussion A subject of discussion 3.x Issue or PR for stable 3.x version labels Jul 8, 2023
@JrooTJunior JrooTJunior added this to the 3.0 milestone Jul 8, 2023
@github-actions
Copy link

github-actions bot commented Jul 8, 2023

✔️ Changelog found.

Thank you for adding a description of the changes

@JrooTJunior JrooTJunior marked this pull request as draft July 8, 2023 00:11
@JrooTJunior JrooTJunior removed the under discussion A subject of discussion label Jul 9, 2023
Refactored base.py to improve code readability by separating response_type operation from model_validate(). Also, adjusted the parameters in URLInputFile() within test_input_file.py for better test coverage. Updated input_file.py to streamline read method and avoid unnecessary instantiation of Bot class. Lastly, adjusted typing in methods/base.py to enhance code clarity.
@codecov
Copy link

codecov bot commented Jul 11, 2023

Codecov Report

Merging #1210 (8e46c57) into dev-3.x (c39a803) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##           dev-3.x     #1210   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          360       361    +1     
  Lines         8960      8973   +13     
=========================================
+ Hits          8960      8973   +13     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
aiogram/types/callback_query.py 100.00% <ø> (ø)
aiogram/types/chat.py 100.00% <ø> (ø)
aiogram/types/chat_join_request.py 100.00% <ø> (ø)
aiogram/types/inline_query.py 100.00% <ø> (ø)
aiogram/types/message.py 100.00% <ø> (ø)
aiogram/types/sticker.py 100.00% <ø> (ø)
aiogram/types/user.py 100.00% <ø> (ø)
aiogram/client/context_controller.py 100.00% <100.00%> (ø)
aiogram/client/session/aiohttp.py 100.00% <100.00%> (ø)
aiogram/client/session/base.py 100.00% <100.00%> (ø)
... and 3 more

@JrooTJunior JrooTJunior marked this pull request as ready for review July 11, 2023 20:17
@JrooTJunior JrooTJunior merged commit a7b92bb into dev-3.x Jul 11, 2023
@JrooTJunior JrooTJunior deleted the bot-context branch August 14, 2023 20:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.x Issue or PR for stable 3.x version
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant