Skip to content

fix: resolve TypeError in SPI/UART tests and fix classmethod_ property chaining - #287

Open
Shikhar-Kesharwani wants to merge 1 commit into
fossasia:mainfrom
Shikhar-Kesharwani:fix/spi-uart-typeerror-270
Open

fix: resolve TypeError in SPI/UART tests and fix classmethod_ property chaining#287
Shikhar-Kesharwani wants to merge 1 commit into
fossasia:mainfrom
Shikhar-Kesharwani:fix/spi-uart-typeerror-270

Conversation

@Shikhar-Kesharwani

@Shikhar-Kesharwani Shikhar-Kesharwani commented Sep 10, 2026

Copy link
Copy Markdown

Fixes #270

Summary

When collecting or running tests/test_spi.py and tests/test_uart.py on Python 3.11+, pytest raised TypeError: unsupported operand type(s) due to arithmetic on SPIMaster._frequency and UART._baudrate.

Root Cause

  1. In pslab/bus/__init__.py, classmethod_.__get__ guarded the evaluation of property descriptors behind if sys.version_info < (3, 9) and isinstance(self.f, property):. In Python 3.11+, chaining classmethod on descriptors was deprecated and in Python 3.13 it was completely removed. As a result, super().__get__(obj, cls) returned a bound method object instead of evaluating the property getter.
  2. PWM_FERQUENCY was misspelled in both tests/test_spi.py and tests/test_uart.py.

Changes

  • Updated classmethod_.__get__ in pslab/bus/__init__.py to always evaluate self.f.__get__(cls) when self.f is an instance of property, ensuring full compatibility across Python versions (3.8 through 3.14+) on both classes and instances. Removed unused sys import.
  • Fixed typo PWM_FERQUENCY -> PWM_FREQUENCY across tests/test_spi.py and tests/test_uart.py.
  • Preserved dynamic calculations (SPIMaster._frequency * 2 / 3 and UART._baudrate // 2) rather than hardcoding frequencies.
  • Added regression tests in tests/test_spi.py and tests/test_uart.py verifying numeric property evaluation and dynamic frequency calculations.

Testing

  • pytest tests/test_spi.py tests/test_uart.py --collect-only (all 25 tests collected successfully)
  • pytest tests/test_spi.py tests/test_uart.py -k "test_frequency or test_clock or test_pwm or test_baudrate" (5 passed)
  • flake8 pslab/bus/__init__.py tests/test_spi.py tests/test_uart.py (clean, 0 warnings)
  • git diff --check (clean, 0 whitespace errors)

Summary by Sourcery

Restore reliable SPI and UART frequency property behavior across Python versions and strengthen regression coverage.

Bug Fixes:

  • Fix classmethod/property descriptor evaluation so SPI and UART numeric properties work across supported Python versions.
  • Correct the PWM frequency constant typo in SPI and UART tests.

Enhancements:

  • Add regression coverage for SPI and UART property values and dynamic PWM frequency calculations.

Tests:

  • Extend SPI and UART tests to verify numeric property evaluation and frequency calculations.

Summary by CodeRabbit

  • Bug Fixes

    • Improved compatibility for property-wrapped class methods across supported Python runtimes.
    • Preserved standard behavior for other descriptor types.
  • Tests

    • Expanded validation for SPI frequency, clock phase, and PWM calculations.
    • Added coverage for UART baud rate properties and related PWM frequency calculations.

- Remove sys.version_info check in classmethod_ descriptor so that properties chained with classmethod_ correctly return evaluated values on Python 3.11+ instead of bound method objects.

- Fix typo PWM_FERQUENCY -> PWM_FREQUENCY in test_spi.py and test_uart.py.

- Add regression tests verifying numeric float/int property evaluation and arithmetic frequency calculations.

Fixes fossasia#270
@sourcery-ai

sourcery-ai Bot commented Sep 10, 2026

Copy link
Copy Markdown

Reviewer's Guide

Updates the custom classmethod descriptor to correctly evaluate chained properties on both classes and instances across Python versions, then fixes SPI/UART test frequency naming and adds regression coverage for numeric property values and dynamic PWM calculations.

Sequence diagram for chained SPI and UART property evaluation

sequenceDiagram
    participant Test
    participant SPIMaster
    participant UART
    participant classmethod_
    participant property

    Test->>SPIMaster: access _frequency
    SPIMaster->>classmethod_: __get__(obj, cls)
    classmethod_ ->> property: __get__(cls)
    property-->>Test: numeric _frequency
    Test->>Test: calculate _frequency * 2 / 3

    Test->>UART: access _baudrate
    UART->>classmethod_: __get__(obj, cls)
    classmethod_ ->> property: __get__(cls)
    property-->>Test: numeric _baudrate
    Test->>Test: calculate _baudrate // 2
Loading

File-Level Changes

Change Details Files
Make chained classmethod/property descriptors evaluate consistently across supported Python versions.
  • Resolve the instance class when the descriptor is accessed without an explicit class.
  • Evaluate property descriptors through the resolved class before falling back to standard classmethod binding.
  • Remove the no-longer-needed Python-version conditional and import.
pslab/bus/__init__.py
Correct SPI and UART test frequency setup and add regression coverage for descriptor evaluation.
  • Fix the PWM frequency constant typo and retain calculations derived from the bus frequency or baud rate.
  • Assert numeric, positive class- and instance-level property values for SPI frequency, UART baud rate, and SPI clock phase.
  • Verify PWM frequency calculations remain dynamically tied to the underlying properties.
tests/test_spi.py
tests/test_uart.py

Assessment against linked issues

Issue Objective Addressed Explanation
#270 Rename the misspelled PWM_FERQUENCY constant to PWM_FREQUENCY throughout the SPI and UART tests.
#270 Fix SPIMaster._frequency and UART._baudrate access so arithmetic uses the evaluated numeric property values rather than method objects, preventing the TypeError.
#270 Keep the test frequency calculations dynamic and derived from the relevant bus properties instead of hardcoding a frequency such as 1000.

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@coderabbitai

coderabbitai Bot commented Sep 10, 2026

Copy link
Copy Markdown

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: d9c49ae2-3b3b-46d4-a334-622d7b13174c

📥 Commits

Reviewing files that changed from the base of the PR and between 08de0d0 and 4fcbb4e.

📒 Files selected for processing (3)
  • pslab/bus/__init__.py
  • tests/test_spi.py
  • tests/test_uart.py

Included review availability: Your plan provides up to 4 included reviews per hour; 3 remain after this review.


📝 Walkthrough

Walkthrough

The change removes a Python-version guard from property-wrapped class method access. It also corrects the PWM_FREQUENCY test constant and adds SPI and UART property and frequency assertions.

Changes

Descriptor access and frequency validation

Layer / File(s) Summary
Property-wrapped class method access
pslab/bus/__init__.py
classmethod_.__get__ handles property-wrapped descriptors without a runtime version check. Other descriptors continue through the superclass.
SPI and UART frequency test corrections
tests/test_spi.py, tests/test_uart.py
The misspelled PWM_FERQUENCY constant is corrected. Tests now validate SPI frequency, clock phase, UART baudrate, and derived PWM frequency values.

Estimated code review effort: 2 (Simple) | ~10 minutes

Severity of issue fixed: Low

Merge Risk: ⚪ Minimal · up to 4fcbb

The descriptor access fix and corrected SPI/UART frequency tests address the reported TypeErrors while preserving dynamic calculations. Current validation shows no merge-blocking risk.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 38.46% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 13 functions across 3 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the two primary changes: resolving SPI/UART test TypeErrors and fixing classmethod_ property chaining.
Linked Issues check ✅ Passed The changes address issue #270 by correcting PWM_FERQUENCY, enabling numeric property evaluation for SPIMaster._frequency and UART._baudrate, preserving dynamic frequency calculations, and adding regr…
Out of Scope Changes check ✅ Passed The changes remain within issue #270. They modify classmethod_ property chaining, correct test identifiers, and add focused regression coverage.
  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

⚠️ This pull request shows signs of AI-generated slop (redundant_comments). It has been flagged by CodeRabbit slop detection and should be reviewed carefully.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've reviewed your changes and they look great!

Sourcery assessment

Approved.


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

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.

Fix TypeError in SPI/UART tests and correct frequency calculation

1 participant