• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

OpShin / opshin / 693

pending completion
693

Pull #126

travis-ci-com

web-flow
Merge 8897aa598 into 137444d4e
Pull Request #126: Fix upcasting by using annotated assignment

15 of 15 new or added lines in 3 files covered. (100.0%)

3278 of 3548 relevant lines covered (92.39%)

3.69 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.31
/opshin/rewrite/rewrite_import_hashlib.py
1
from typing import Optional
4✔
2
from enum import Enum
4✔
3

4
from ..util import CompilingNodeTransformer
4✔
5
from ..typed_ast import *
4✔
6

7
"""
2✔
8
Checks that there was an import of dataclass if there are any class definitions
9
"""
10

11

12
@dataclass(frozen=True, unsafe_hash=True)
4✔
13
class HashType(ClassType):
4✔
14
    """A pseudo class that is the result of python hash functions that need a 'digest' call"""
15

16
    def attribute_type(self, attr) -> "Type":
4✔
17
        if attr == "digest":
4✔
18
            return InstanceType(FunctionType([], ByteStringInstanceType))
4✔
19
        raise NotImplementedError("HashType only has attribute 'digest'")
×
20

21
    def attribute(self, attr) -> plt.AST:
4✔
22
        if attr == "digest":
4✔
23
            return plt.Lambda(["self"], plt.Var("self"))
4✔
24
        raise NotImplementedError("HashType only has attribute 'digest'")
×
25

26
    def __ge__(self, other):
4✔
27
        return isinstance(other, HashType)
×
28

29

30
HashInstanceType = InstanceType(HashType())
4✔
31

32

33
class PythonHashlib(Enum):
4✔
34
    sha256 = plt.Lambda(["x", "_"], plt.Lambda(["_"], plt.Sha2_256(plt.Var("x"))))
4✔
35
    sha3_256 = plt.Lambda(["x", "_"], plt.Lambda(["_"], plt.Sha3_256(plt.Var("x"))))
4✔
36
    blake2b = plt.Lambda(["x", "_"], plt.Lambda(["_"], plt.Blake2b_256(plt.Var("x"))))
4✔
37

38

39
PythonHashlibTypes = {
4✔
40
    PythonHashlib.sha256: InstanceType(
41
        FunctionType(
42
            [ByteStringInstanceType],
43
            HashInstanceType,
44
        )
45
    ),
46
    PythonHashlib.sha3_256: InstanceType(
47
        FunctionType(
48
            [ByteStringInstanceType],
49
            HashInstanceType,
50
        )
51
    ),
52
    PythonHashlib.blake2b: InstanceType(
53
        FunctionType(
54
            [ByteStringInstanceType],
55
            HashInstanceType,
56
        )
57
    ),
58
}
59

60

61
class RewriteImportHashlib(CompilingNodeTransformer):
4✔
62
    step = "Resolving imports and usage of hashlib"
4✔
63

64
    imports_hashlib = False
4✔
65

66
    def visit_ImportFrom(self, node: ImportFrom) -> typing.List[AST]:
4✔
67
        if node.module != "hashlib":
4✔
68
            return node
4✔
69
        additional_assigns = []
4✔
70
        for n in node.names:
4✔
71
            imported_fun = None
4✔
72
            for h in PythonHashlib:
4✔
73
                if h.name == n.name:
4✔
74
                    imported_fun = h
4✔
75
            assert (
4✔
76
                imported_fun is not None
77
            ), f"Unsupported function import from hashlib '{n.name}"
78
            typ = PythonHashlibTypes[imported_fun]
4✔
79
            imported_name = n.name if n.asname is None else n.asname
4✔
80
            additional_assigns.append(
4✔
81
                TypedAssign(
82
                    targets=[TypedName(id=imported_name, typ=typ, ctx=Store())],
83
                    value=RawPlutoExpr(
84
                        typ=typ, expr=plt.Lambda(["_"], imported_fun.value)
85
                    ),
86
                )
87
            )
88
        return additional_assigns
4✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc