Skip to content

Conversation

@youtux
Copy link
Contributor

@youtux youtux commented Dec 2, 2023

Fix many mypy issues.
Notably, I replace private attributes like __scenario__, __scenario_report__, etc. with registries (WeakKeyDictionary). E.g.

# Old way

# setting value
scenario_wrapper.__scenario__ = scenario

# getting value
scenario = getattr(obj, "__scenario__", None)


# New way
# setting value
scenario_wrapper_template_registry[scenario_wrapper] = scenario

# getting value
scenario = scenario_wrapper_template_registry.get(scenario_wrapper)

Other changes:

  • Replaced usages of Any with a more specific type if possible, otherwise object. Any causes the type checker to completely forego the type checking. Using object is a better way.

@codecov
Copy link

codecov bot commented Dec 2, 2023

Codecov Report

Attention: Patch coverage is 94.25837% with 12 lines in your changes missing coverage. Please review.

Project coverage is 96.01%. Comparing base (6cdd340) to head (f4413e5).
Report is 65 commits behind head on master.

Files with missing lines Patch % Lines
src/pytest_bdd/cucumber_json.py 93.10% 3 Missing and 1 partial ⚠️
src/pytest_bdd/generation.py 57.14% 1 Missing and 2 partials ⚠️
tests/feature/test_report.py 81.25% 3 Missing ⚠️
src/pytest_bdd/utils.py 87.50% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #658      +/-   ##
==========================================
- Coverage   96.08%   96.01%   -0.08%     
==========================================
  Files          55       55              
  Lines        2250     2359     +109     
  Branches      246      250       +4     
==========================================
+ Hits         2162     2265     +103     
- Misses         53       56       +3     
- Partials       35       38       +3     

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

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@youtux youtux requested a review from drothlis January 21, 2024 13:51
@youtux youtux marked this pull request as ready for review January 21, 2024 13:51
sourcery-ai[bot]

This comment was marked as outdated.

@youtux youtux requested a review from olegpidsadnyi January 21, 2024 13:53
@jsa34
Copy link
Collaborator

jsa34 commented Sep 22, 2024

@youtux I think mypy is happy now since we merged #698 ? If it is, perhaps we can close this?

@youtux
Copy link
Contributor Author

youtux commented Sep 22, 2024

Well, mypy is happy only because we put a lot of #type: ignore, ideally we wouldn't need those

@jsa34
Copy link
Collaborator

jsa34 commented Sep 22, 2024

Good point - so the focus of this would be to eliminate those where possible :)

# Conflicts:
#	poetry.lock
#	src/pytest_bdd/cucumber_json.py
#	src/pytest_bdd/feature.py
#	src/pytest_bdd/generation.py
#	src/pytest_bdd/gherkin_terminal_reporter.py
#	src/pytest_bdd/parser.py
#	src/pytest_bdd/reporting.py
#	src/pytest_bdd/scenario.py
#	src/pytest_bdd/steps.py
#	src/pytest_bdd/utils.py
#	tests/feature/test_description.py
@youtux youtux requested a review from jsa34 December 1, 2024 15:03
I managed to remove all occurrences of `Any`, and use proper typed dicts instead
jsa34
jsa34 previously approved these changes Dec 5, 2024
@youtux youtux merged commit 9bb4967 into master Dec 5, 2024
16 of 18 checks passed
@youtux youtux deleted the ab/fix-typing branch December 5, 2024 22:27
@youtux youtux requested a review from Copilot December 12, 2024 20:12
Copy link

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.

Copilot reviewed 5 out of 14 changed files in this pull request and generated no suggestions.

Files not reviewed (9)
  • CHANGES.rst: Language not supported
  • src/pytest_bdd/steps.py: Evaluated as low risk
  • src/pytest_bdd/cucumber_json.py: Evaluated as low risk
  • src/pytest_bdd/feature.py: Evaluated as low risk
  • src/pytest_bdd/compat.py: Evaluated as low risk
  • tests/feature/test_report.py: Evaluated as low risk
  • src/pytest_bdd/generation.py: Evaluated as low risk
  • src/pytest_bdd/parser.py: Evaluated as low risk
  • src/pytest_bdd/plugin.py: Evaluated as low risk
Comments skipped due to low confidence (4)

src/pytest_bdd/utils.py:99

  • The default parameter should be typed as V | T | None to match the return type and avoid confusion.
def registry_get_safe(registry: WeakKeyDictionary[K, V], key: object, default: T | None = None) -> T | V | None:

src/pytest_bdd/scenario.py:289

  • The type annotation for the decorator function is incorrect. It should be updated to match the new signature.
) -> Callable[[Callable[P, T]], Callable[P, T]]:

src/pytest_bdd/scenario.py:471

  • [nitpick] The variable name 's' within the generator expression is not descriptive. It should be renamed to something more meaningful.
if (s := registry_get_safe(scenario_wrapper_template_registry, attr)) is not None

src/pytest_bdd/gherkin_terminal_reporter.py:77

  • Ensure that scenario is not None before accessing its attributes to prevent potential AttributeError.
if self.verbosity <= 0 or scenario is None:

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