mirror of
https://github.com/pybind/pybind11.git
synced 2026-04-20 06:49:25 +00:00
feat(typing): allow annotate methods with pos_only when only have the self argument (#5403)
* feat: allow annotate methods with `pos_only` when only have the `self` argument * chore(typing): make arguments for auto-generated dunder methods positional-only * docs: add more comments to improve readability * style: fix nit suggestions * Add test_self_only_pos_only() in tests/test_methods_and_attributes * test: add docstring tests for generated dunder methods * test: remove failed tests * fix(test): run `gc.collect()` three times for refcount tests --------- Co-authored-by: Ralf W. Grosse-Kunstleve <rgrossekunst@nvidia.com>
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
# ruff: noqa: SIM201 SIM300 SIM202
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
|
||||
import pytest
|
||||
|
||||
import env # noqa: F401
|
||||
@@ -271,3 +273,61 @@ def test_docstring_signatures():
|
||||
def test_str_signature():
|
||||
for enum_type in [m.ScopedEnum, m.UnscopedEnum]:
|
||||
assert enum_type.__str__.__doc__.startswith("__str__")
|
||||
|
||||
|
||||
def test_generated_dunder_methods_pos_only():
|
||||
for enum_type in [m.ScopedEnum, m.UnscopedEnum]:
|
||||
for binary_op in [
|
||||
"__eq__",
|
||||
"__ne__",
|
||||
"__ge__",
|
||||
"__gt__",
|
||||
"__lt__",
|
||||
"__le__",
|
||||
"__and__",
|
||||
"__rand__",
|
||||
# "__or__", # fail with some compilers (__doc__ = "Return self|value.")
|
||||
# "__ror__", # fail with some compilers (__doc__ = "Return value|self.")
|
||||
"__xor__",
|
||||
"__rxor__",
|
||||
"__rxor__",
|
||||
]:
|
||||
method = getattr(enum_type, binary_op, None)
|
||||
if method is not None:
|
||||
assert (
|
||||
re.match(
|
||||
rf"^{binary_op}\(self: [\w\.]+, other: [\w\.]+, /\)",
|
||||
method.__doc__,
|
||||
)
|
||||
is not None
|
||||
)
|
||||
for unary_op in [
|
||||
"__int__",
|
||||
"__index__",
|
||||
"__hash__",
|
||||
"__str__",
|
||||
"__repr__",
|
||||
]:
|
||||
method = getattr(enum_type, unary_op, None)
|
||||
if method is not None:
|
||||
assert (
|
||||
re.match(
|
||||
rf"^{unary_op}\(self: [\w\.]+, /\)",
|
||||
method.__doc__,
|
||||
)
|
||||
is not None
|
||||
)
|
||||
assert (
|
||||
re.match(
|
||||
r"^__getstate__\(self: [\w\.]+, /\)",
|
||||
enum_type.__getstate__.__doc__,
|
||||
)
|
||||
is not None
|
||||
)
|
||||
assert (
|
||||
re.match(
|
||||
r"^__setstate__\(self: [\w\.]+, state: [\w\.]+, /\)",
|
||||
enum_type.__setstate__.__doc__,
|
||||
)
|
||||
is not None
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user