josiahcarlson/rom

mypy compliance when using custom column classes

Opened this issue · 1 comments

Hello,

Thanks for the awesome library! I would like to ask how can we allow custom classes derived from Column to pass mypy type checking. More concretely, let us suppose we have the following (simple?) class:

class TransformedString(rom.Column):
    """
    Transformation on strings
    """

    _allowed = bytes

    def _to_redis(self, value):
        return value

    def _from_redis(self, value):
        return value

and suppose that we have the following ORM model:

class MyModel(rom.Model):
    """Entry representing MyModel"""

    id = rom.PrimaryKey(index=True)
    number = rom.Integer()
    html = TransformedString()

Now if I go and assign values as follows, while it works mypy throws an error.

# assume an instance of `MyModel`
my_model.number = 1 # works!
my_model.html = str.encode("a_nice_long_string_with_many_features") # mypy throws an error

The error mypy throws is the following:

error: Incompatible types in assignment (expression has type "bytes", variable has type "TransformedString")

How am I able to fix this without resorting to ignoring the types altogether?