bpo-45663: Fix is_dataclass() for dataclasses which are subclasses of types.GenericAlias (GH-29294)

This commit is contained in:
Serhiy Storchaka 2021-12-05 22:42:50 +02:00 committed by GitHub
parent 1fd4de5bdd
commit 446be16686
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 1 deletions

View file

@ -8,6 +8,7 @@
import pickle
import inspect
import builtins
import types
import unittest
from unittest.mock import Mock
from typing import ClassVar, Any, List, Union, Tuple, Dict, Generic, TypeVar, Optional, Protocol
@ -1354,6 +1355,17 @@ class B:
with self.assertRaisesRegex(TypeError, 'should be called on dataclass instances'):
replace(obj, x=0)
def test_is_dataclass_genericalias(self):
@dataclass
class A(types.GenericAlias):
origin: type
args: type
self.assertTrue(is_dataclass(A))
a = A(list, int)
self.assertTrue(is_dataclass(type(a)))
self.assertTrue(is_dataclass(a))
def test_helper_fields_with_class_instance(self):
# Check that we can call fields() on either a class or instance,
# and get back the same thing.