Skip to content

Refactor FSM Storage methods input types to calm down MyPy.#1683

Merged
JrooTJunior merged 3 commits intoaiogram:dev-3.xfrom
andrew000:storage-mapping-type
May 16, 2025
Merged

Refactor FSM Storage methods input types to calm down MyPy.#1683
JrooTJunior merged 3 commits intoaiogram:dev-3.xfrom
andrew000:storage-mapping-type

Conversation

@andrew000
Copy link
Contributor

@andrew000 andrew000 commented Apr 28, 2025

#1682

Description

Refactor FSM Storage methods input types to calm down MyPy.

Refactored:

  • FSMContext.set_data
  • FSMContext.update_data
  • BaseStorage.set_data
  • BaseStorage.update_data
  • BaseStorage's child methods
  • SceneWizard.set_data
  • SceneWizard.update_data

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • This change requires a documentation update

How Has This Been Tested?

1 test edited to cover new code

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
  • 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

- `FSMContext.set_data`
- `FSMContext.update_data`
- `BaseStorage.set_data`
- `BaseStorage.update_data`
- `BaseStorage`'s child methods
- `SceneWizard.set_data`
- `SceneWizard.update_data`
@github-actions github-actions bot added the 3.x Issue or PR for stable 3.x version label Apr 28, 2025
@github-actions
Copy link

github-actions bot commented Apr 28, 2025

✔️ Changelog found.

Thank you for adding a description of the changes

@JrooTJunior JrooTJunior requested a review from Copilot April 28, 2025 08:39
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors methods’ input types across FSM storage and scene modules to address MyPy issues while adding runtime validation for dict-like data. Key changes include updating type annotations from Dict[str, Any] to Mapping[str, Any], adding explicit type checks that raise DataNotDictLikeError, and extending test coverage for these new validations.

Reviewed Changes

Copilot reviewed 8 out of 9 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
tests/test_fsm/storage/test_storages.py Added tests using TypedDict and validation for non-dict data types
aiogram/fsm/storage/redis.py Updated set_data signature and added a runtime check for dict types
aiogram/fsm/storage/mongo.py Updated set_data and update_data signatures with runtime validations
aiogram/fsm/storage/memory.py Updated set_data signature with an added type check
aiogram/fsm/storage/base.py Adjusted abstract type hints for data parameters
aiogram/fsm/scene.py Updated scene methods’ parameter annotations and documentation
aiogram/fsm/context.py Updated context methods to use Mapping for set_data and update_data
aiogram/exceptions.py Added DataNotDictLikeError for invalid data inputs
Files not reviewed (1)
  • CHANGES/1683.feature.rst: Language not supported

@JrooTJunior JrooTJunior changed the title Refactor methods input types to calm down MyPy. Refactor FSM Storage methods input types to calm down MyPy. Apr 28, 2025
@andrew000 andrew000 requested a review from Copilot April 28, 2025 08:47
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors the input types for FSM storage methods to calm down MyPy and adds runtime validations for data passed to these methods. Key changes include updating function signatures from Dict[str, Any] to Mapping[str, Any] for storage methods, adding runtime checks to ensure that data is a dict, and updating tests and exceptions accordingly.

Reviewed Changes

Copilot reviewed 8 out of 9 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
tests/test_fsm/storage/test_storages.py Added tests to cover TypedDict inputs and exception handling for invalid data.
aiogram/fsm/storage/redis.py Updated type hints for set_data and added a runtime check to enforce dict type.
aiogram/fsm/storage/mongo.py Updated type hints and added a runtime check in set_data to verify dict type.
aiogram/fsm/storage/memory.py Updated set_data signature to accept Mapping and enforced a dict copy after check.
aiogram/fsm/storage/base.py Updated abstract methods for set_data and update_data to accept Mapping.
aiogram/fsm/scene.py & aiogram/fsm/context.py Updated method signatures to use Mapping where applicable.
aiogram/exceptions.py Introduced DataNotDictLikeError for enforcing dict-like input validations.
Files not reviewed (1)
  • CHANGES/1683.feature.rst: Language not supported

@codecov
Copy link

codecov bot commented Apr 28, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (482629a) to head (19250ee).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff            @@
##           dev-3.x     #1683   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          568       568           
  Lines        13341     13351   +10     
=========================================
+ Hits         13341     13351   +10     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

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

Files with missing lines Coverage Δ
aiogram/exceptions.py 100.00% <100.00%> (ø)
aiogram/fsm/context.py 100.00% <100.00%> (ø)
aiogram/fsm/scene.py 100.00% <100.00%> (ø)
aiogram/fsm/storage/base.py 100.00% <100.00%> (ø)
aiogram/fsm/storage/memory.py 100.00% <100.00%> (ø)
aiogram/fsm/storage/mongo.py 100.00% <100.00%> (ø)
aiogram/fsm/storage/redis.py 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@andrew000 andrew000 force-pushed the storage-mapping-type branch from 01b98d0 to 19250ee Compare April 28, 2025 12:09
@JrooTJunior JrooTJunior merged commit e011d10 into aiogram:dev-3.x May 16, 2025
44 checks passed
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.

3 participants