Fix issubclass check (#618)

This commit is contained in:
Nikhil Rao 2023-02-28 20:10:00 -08:00 committed by GitHub
parent dac52da780
commit 46cdda4125
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 3 deletions

View File

@ -159,8 +159,16 @@ def _issubclass(cls: GenericType, cls_check: GenericType) -> bool:
return True return True
if cls in [Any, Callable]: if cls in [Any, Callable]:
return False return False
# Get the base classes.
cls_base = get_base_class(cls) cls_base = get_base_class(cls)
cls_check_base = get_base_class(cls_check) cls_check_base = get_base_class(cls_check)
# The class we're checking should not be a union.
if isinstance(cls_base, tuple):
return False
# Check if the types match.
return cls_check_base == Any or issubclass(cls_base, cls_check_base) return cls_check_base == Any or issubclass(cls_base, cls_check_base)

View File

@ -1,8 +1,9 @@
import typing from typing import Any, List, Union
import pytest import pytest
from pynecone import utils from pynecone import utils
from pynecone.var import Var
@pytest.mark.parametrize( @pytest.mark.parametrize(
@ -195,8 +196,8 @@ def test_merge_imports():
(int, False), (int, False),
(float, False), (float, False),
(bool, False), (bool, False),
(typing.List, True), (List, True),
(typing.List[int], True), (List[int], True),
], ],
) )
def test_is_generic_alias(cls: type, expected: bool): def test_is_generic_alias(cls: type, expected: bool):
@ -261,3 +262,24 @@ def test_setup_frontend(tmp_path, mocker):
) )
def test_is_backend_variable(input, output): def test_is_backend_variable(input, output):
assert utils.is_backend_variable(input) == output assert utils.is_backend_variable(input) == output
@pytest.mark.parametrize(
"cls, cls_check, expected",
[
(int, int, True),
(int, float, False),
(int, Union[int, float], True),
(float, Union[int, float], True),
(str, Union[int, float], False),
(List[int], List[int], True),
(List[int], List[float], True),
(Union[int, float], Union[int, float], False),
(Union[int, Var[int]], Var[int], False),
(int, Any, True),
(Any, Any, True),
(Union[int, float], Any, True),
],
)
def test_issubclass(cls: type, cls_check: type, expected: bool):
assert utils._issubclass(cls, cls_check) == expected