Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Also test typing.NamedTuple with copy.replace #109956

Closed
sobolevn opened this issue Sep 27, 2023 · 1 comment
Closed

Also test typing.NamedTuple with copy.replace #109956

sobolevn opened this issue Sep 27, 2023 · 1 comment
Assignees
Labels
tests Tests in the Lib/test dir type-bug An unexpected behavior, bug, or error

Comments

@sobolevn
Copy link
Member

sobolevn commented Sep 27, 2023

Bug report

Right now only collections.namedtuple is tested:

def test_namedtuple(self):
from collections import namedtuple
Point = namedtuple('Point', 'x y', defaults=(0,))
p = Point(11, 22)
self.assertEqual(copy.replace(p), (11, 22))
self.assertEqual(copy.replace(p, x=1), (1, 22))
self.assertEqual(copy.replace(p, y=2), (11, 2))
self.assertEqual(copy.replace(p, x=1, y=2), (1, 2))
with self.assertRaisesRegex(ValueError, 'unexpected field name'):
copy.replace(p, x=1, error=2)

@serhiy-storchaka noted in #108752 (comment) that only named tuples created by collections.namedtuple() are supported. But, since typing.NamedTuple uses collections.namedtuple inside:

cpython/Lib/typing.py

Lines 2695 to 2696 in b1aebf1

nm_tpl = collections.namedtuple(name, fields,
defaults=defaults, module=module)
it is also supported:

>>> import typing
>>> 
>>> class N(typing.NamedTuple):
...    x: int
...    y: int
... 
>>> N.__replace__
<function N._replace at 0x10580a210>
>>> import copy
>>> copy.replace(N(1, 2), x=3)
N(x=3, y=2)

I have a PR ready with extra tests.

Refs #108751
Refs #108752

Linked PRs

@sobolevn sobolevn added type-bug An unexpected behavior, bug, or error tests Tests in the Lib/test dir labels Sep 27, 2023
@sobolevn sobolevn self-assigned this Sep 27, 2023
sobolevn added a commit to sobolevn/cpython that referenced this issue Sep 27, 2023
@serhiy-storchaka
Copy link
Member

"Named tuple" is more general term (https://docs.python.org/3/glossary.html#term-named-tuple). Not all named tuples in wide meaning are supported (for example named tuples created by the PyStructSequence C API are not supported yet). typing.NamedTyple is a funny way to call collections.namedtuple(), so this note covers it too. If you want to test them, would not be better to add new tests to other typing.NamedTyple tests?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tests Tests in the Lib/test dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants