Skip to content

invalid-bytes-return-type (PLE0308)#

Derived from the Pylint linter.

This rule is unstable and in preview. The --preview flag is required for use.

What it does#

Checks for __bytes__ implementations that return a type other than bytes.

Why is this bad?#

The __bytes__ method should return a bytes object. Returning a different type may cause unexpected behavior.


class Foo:
    def __bytes__(self):
        return 2

Use instead:

class Foo:
    def __bytes__(self):
        return b"2"