Skip to content

Type pytest.approx#14373

Merged
bluetech merged 2 commits intopytest-dev:mainfrom
MarcoGorelli:type-approx
Apr 10, 2026
Merged

Type pytest.approx#14373
bluetech merged 2 commits intopytest-dev:mainfrom
MarcoGorelli:type-approx

Conversation

@MarcoGorelli
Copy link
Copy Markdown
Contributor

No description provided.

@psf-chronographer psf-chronographer bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Apr 9, 2026

def approx(expected, rel=None, abs=None, nan_ok: bool = False) -> ApproxBase:
def approx(
expected: Any,
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

this one's extremely dynamic and can accept a huge variety of arguments, e.g.

class MyVec3: # incomplete
"""sequence like"""
_x: int
_y: int
_z: int
def __init__(self, x: int, y: int, z: int):
self._x, self._y, self._z = x, y, z
def __repr__(self) -> str:
return f"<MyVec3 {self._x} {self._y} {self._z}>"
def __len__(self) -> int:
return 3
def __getitem__(self, key: int) -> int:
if key == 0:
return self._x
if key == 1:
return self._y
if key == 2:
return self._z
raise IndexError(key)

Not sure how much it's worth it to list everything, so for now I'm just suggesting Any here and we can start making rel and abs more precise as well as making the function typed

Comment on lines +619 to +620
assert approx(x, rel=0, abs=Decimal("5e-3")) == a
assert approx(x, rel=0, abs=Decimal("5e-7")) != a
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I noticed that there wasn't any test for abs: Decimal, so I added this one

@MarcoGorelli MarcoGorelli marked this pull request as ready for review April 9, 2026 10:56
Copy link
Copy Markdown
Member

@bluetech bluetech left a comment

Choose a reason for hiding this comment

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

The Any looks fine for now. Thanks!

@bluetech bluetech enabled auto-merge (squash) April 10, 2026 06:52
@bluetech bluetech merged commit 694ffff into pytest-dev:main Apr 10, 2026
33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants