This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with self.assertRaisesRegex(ValueError, 'init=False'):
replace(c, x=3, z=20, t=50)
with self.assertRaisesRegex(ValueError, 'init=False'):
replace(c, z=20)
replace(c, x=3, z=20, t=50)
# Make sure the result is still frozen.
with self.assertRaisesRegex(FrozenInstanceError, "cannot assign to field 'x'"):
c1.x = 3
# Make sure we can't replace an attribute that doesn't exist,
# if we're also replacing one that does exist. Test this
# here, because setting attributes on frozen instances is
# handled slightly differently from non-frozen ones.
with self.assertRaisesRegex(TypeError, r"__init__\(\) got an unexpected "
"keyword argument 'a'"):
c1 = replace(c, x=20, a=5)
def test_invalid_field_name(self):
@dataclass(frozen=True)
class C:
x: int
y: int
c = C(1, 2)
with self.assertRaisesRegex(TypeError, r"__init__\(\) got an unexpected "
"keyword argument 'z'"):
c1 = replace(c, z=3)
def test_invalid_object(self):
@dataclass(frozen=True)
class C:
x: int
y: int
with self.assertRaisesRegex(TypeError, 'dataclass instance'):
replace(C, x=3)
with self.assertRaisesRegex(TypeError, 'dataclass instance'):
replace(0, x=3)
def test_no_init(self):
@dataclass
class C:
x: int
y: int = field(init=False, default=10)
c = C(1)
c.y = 20
# Make sure y gets the default value.
c1 = replace(c, x=5)
self.assertEqual((c1.x, c1.y), (5, 10))
# Trying to replace y is an error.
with self.assertRaisesRegex(ValueError, 'init=False'):
replace(c, x=2, y=30)
with self.assertRaisesRegex(ValueError, 'init=False'):
replace(c, y=30)
def test_classvar(self):
@dataclass
class C:
x: int
y: ClassVar[int] = 1000
c = C(1)
d = C(2)
self.assertIs(c.y, d.y)
self.assertEqual(c.y, 1000)
# Trying to replace y is an error: can't replace ClassVars.
with self.assertRaisesRegex(TypeError, r"__init__\(\) got an "
"unexpected keyword argument 'y'"):
replace(c, y=30)
replace(c, x=5)
## def test_initvar(self):
## @dataclass
## class C:
## x: int
## y: InitVar[int]
## c = C(1, 10)
## d = C(2, 20)
## # In our case, replacing an InitVar is a no-op
## self.assertEqual(c, replace(c, y=5))
## replace(c, x=5)
if __name__ == '__main__':
unittest.main()
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
[3.7] bpo-33796: Ignore ClassVar for dataclasses.replace(). (GH-7488) #7493
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
Uh oh!
There was an error while loading. Please reload this page.
[3.7] bpo-33796: Ignore ClassVar for dataclasses.replace(). (GH-7488) #7493
Filter by extension
Viewed files
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
There are no files selected for viewing