josiahcarlson / rom

Redis object mapper for Python
GNU Lesser General Public License v2.1
360 stars 49 forks source link

mypy compliance when using custom column classes #155

Open andylamp opened 2 years ago

andylamp commented 2 years ago

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?

josiahcarlson commented 1 year ago

I haven't done any work to make Rom specifically mypy compliant in any way.

I'll have to look at how other libraries do this, and how mypy recommends solving this when I get back to a computer in January.

On Sun, Feb 13, 2022, 9:34 AM andylamp @.***> wrote:

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?

for example, 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 a GitHub issue"""

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 MyModelmy_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?

— Reply to this email directly, view it on GitHub https://github.com/josiahcarlson/rom/issues/155, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABTDQQMA3PZFWKYPQUPHK3U27FRPANCNFSM5OJF33GA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you are subscribed to this thread.Message ID: @.***>