Skip to content

Removed implicit singleton behavior of Tolerance.#1510

Open
chenkasirer wants to merge 1 commit intomainfrom
tolerance
Open

Removed implicit singleton behavior of Tolerance.#1510
chenkasirer wants to merge 1 commit intomainfrom
tolerance

Conversation

@chenkasirer
Copy link
Member

Closes #1509

  • Removed Singleton behavior of Tolerance which was leading to accidental global state modification.
  • Tolerance(...) now creates independent instances of Tolerance
  • Added Tolerance.update() to update an instance in-place (e.g. TOL)
  • Added Tolerance.temporary() context manager for local modifications of tolerance.

What type of change is this?

  • Bug fix in a backwards-compatible manner.
  • New feature in a backwards-compatible manner.
  • Breaking change: bug fix or new feature that involve incompatible API changes.
  • Other (e.g. doc update, configuration, etc)

Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code.

  • I added a line to the CHANGELOG.md file in the Unreleased section under the most fitting heading (e.g. Added, Changed, Removed).
  • I ran all tests on my computer and it's all green (i.e. invoke test).
  • I ran lint on my computer and there are no errors (i.e. invoke lint).
  • I added new functions/classes and made them available on a second-level import, e.g. compas.datastructures.Mesh.
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added necessary documentation (if appropriate)

…es can be created and global instance can be explicitly modified
@chenkasirer chenkasirer requested a review from Copilot February 4, 2026 09:32
@chenkasirer chenkasirer changed the title removed implicit singleton behavior of Tolerance. Removed implicit singleton behavior of Tolerance. Feb 4, 2026
@codecov
Copy link

codecov bot commented Feb 4, 2026

Codecov Report

❌ Patch coverage is 81.81818% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 62.30%. Comparing base (e34268a) to head (a8f0ad6).

Files with missing lines Patch % Lines
src/compas/tolerance.py 81.81% 8 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1510      +/-   ##
==========================================
+ Coverage   62.26%   62.30%   +0.04%     
==========================================
  Files         208      208              
  Lines       22452    22475      +23     
==========================================
+ Hits        13979    14003      +24     
+ Misses       8473     8472       -1     

☔ 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.

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 removes the implicit singleton behavior of Tolerance, so Tolerance(...) now returns independent instances while TOL remains the explicit global instance. It also introduces explicit APIs for mutating global tolerance state (TOL.update and TOL.temporary) and documents these behaviors, plus updates the changelog and tests accordingly.

Changes:

  • Refactored compas.tolerance.Tolerance to be a regular class (no singleton), while keeping a module-level TOL = Tolerance() as the shared global instance.
  • Added Tolerance.update(...) and Tolerance.temporary(...) to support explicit, scoped modifications of tolerance settings, and updated module/class docstrings.
  • Extended tests/compas/test_tolerance.py and CHANGELOG.md to cover and document the new behavior.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/compas/tolerance.py Removes singleton machinery, adds explicit mutation helpers (update, temporary), extends documentation, and keeps TOL as a dedicated global instance.
tests/compas/test_tolerance.py Adds tests to verify independent Tolerance instances, explicit TOL.update, and scoped TOL.temporary behavior (including restoration on exceptions).
CHANGELOG.md Documents the new TOL.update/TOL.temporary APIs and the change away from the singleton Tolerance pattern.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +400 to +404
saved = {
"unit": self._unit,
"absolute": self._absolute,
"relative": self._relative,
"angular": self._angular,
Copy link

Copilot AI Feb 4, 2026

Choose a reason for hiding this comment

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

temporary() saves and restores self._unit, but updates the unit via self.update(unit=...), which assigns to self.unit (a plain instance attribute, since there is no unit property). As a result, using TOL.temporary(unit=...) will change TOL.unit permanently instead of reverting it on context exit, unlike the other fields that correctly round-trip via their private attributes. To make the context manager behave consistently and avoid accidental global state changes for unit, the implementation should either snapshot and restore self.unit (not self._unit) or introduce a proper unit property that proxies to _unit and make both update and temporary use that.

Copilot uses AI. Check for mistakes.
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.

Proposal: changes to the behavior or Tolerance/TOL

1 participant