summaryrefslogtreecommitdiff
path: root/tests/functional/i/invalid/invalid_bytes_returned.py
blob: 993bd87771357328de1d08994afede80fab0eb23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""Check invalid value returned by __bytes__ """

# pylint: disable=too-few-public-methods,missing-docstring,import-error,useless-object-inheritance,unnecessary-lambda-assignment
import six

from missing import Missing


class FirstGoodBytes(object):
    """__bytes__ returns <type 'bytes'>"""

    def __bytes__(self):
        return b"some bytes"


class SecondGoodBytes(object):
    """__bytes__ returns <type 'bytes'>"""

    def __bytes__(self):
        return bytes("123", "ascii")


class BytesMetaclass(type):
    def __bytes__(cls):
        return b"some bytes"


@six.add_metaclass(BytesMetaclass)
class ThirdGoodBytes(object):
    """Bytes through the metaclass."""


class FirstBadBytes(object):
    """ __bytes__ returns bytes """

    def __bytes__(self):  # [invalid-bytes-returned]
        return "123"


class SecondBadBytes(object):
    """ __bytes__ returns int """

    def __bytes__(self):  # [invalid-bytes-returned]
        return 1


class ThirdBadBytes(object):
    """ __bytes__ returns node which does not have 'value' in AST """

    def __bytes__(self):  # [invalid-bytes-returned]
        return lambda: b"some bytes"


class AmbiguousBytes(object):
    """ Uninferable return value """

    __bytes__ = lambda self: Missing


class AnotherAmbiguousBytes(object):
    """Potential uninferable return value"""

    def __bytes__(self):
        return bytes(Missing)