Skip to content

Commit

Permalink
Add support for NewType
Browse files Browse the repository at this point in the history
  • Loading branch information
pschanely committed Mar 24, 2024
1 parent e698c34 commit 5229acd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
4 changes: 3 additions & 1 deletion crosshair/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
Collection,
Dict,
FrozenSet,
Generator,
Iterable,
List,
Mapping,
MutableMapping,
NewType,
Optional,
Sequence,
Set,
Expand Down Expand Up @@ -604,6 +604,8 @@ def proxy_for_type(
if proxy_factory:
recursive_proxy_factory = SymbolicFactory(space, typ, varname)
return proxy_factory(recursive_proxy_factory, *type_args)
if hasattr(typ, "__supertype__") and typing_inspect.is_new_type(typ):
return proxy_for_type(typ.__supertype__, varname, allow_subtypes) # type: ignore
if allow_subtypes and typ is not object:
typ = choose_type(space, typ, varname)
if typ is None: # (happens if typ and all subtypes are abstract)
Expand Down
8 changes: 8 additions & 0 deletions crosshair/core_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
standalone_statespace,
)
from crosshair.fnutil import FunctionInfo, walk_qualname
from crosshair.libimpl.builtinslib import SymbolicInt
from crosshair.options import DEFAULT_OPTIONS, AnalysisOptionSet
from crosshair.statespace import (
CANNOT_CONFIRM,
Expand Down Expand Up @@ -705,6 +706,13 @@ def signaling_alarm(self, air_samples: List[int]) -> bool:
)
)

def test_newtype(self) -> None:
T = TypeVar("T")
Number = NewType("Number", int)
with standalone_statespace:
x = proxy_for_type(Number, "x", allow_subtypes=False)
assert isinstance(x, SymbolicInt)

def test_container_typevar(self) -> None:
T = TypeVar("T")

Expand Down

0 comments on commit 5229acd

Please sign in to comment.