Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mypy compliance when using custom column classes #155

Open
andylamp opened this issue Feb 13, 2022 · 1 comment
Open

mypy compliance when using custom column classes #155

andylamp opened this issue Feb 13, 2022 · 1 comment

Comments

@andylamp
Copy link

andylamp commented Feb 13, 2022

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?

@andylamp andylamp changed the title mypy compliance when using custom columns classes mypy compliance when using custom column classes Feb 13, 2022
@josiahcarlson
Copy link
Owner

josiahcarlson commented Dec 27, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants