typeddjango / django-stubs

PEP-484 stubs for Django
MIT License
1.6k stars 450 forks source link

Strings and Bitwise operators #357

Open dfarley1 opened 4 years ago

dfarley1 commented 4 years ago

I'm using the netfields (link) package and have a simple model:

class MACAddressBlock(models.Model):
    prefix = netfields.MACAddressField(primary_key=True)
    mask = netfields.MACAddressField()

Given an entry like prefix="00:50:C2:00:00:00", mask="ff:ff:ff:00:00:00" I want to query with a MAC Address mac="00:50:C2:12:34:56" and find all prefixes which could contain the address. So I do a query like MACAddressBlock.filter(prefix=F("mask").bitand(mac)).

This works in all my test cases, but Mypy complains that Argument 1 to "bitand" of "Combinable" has incompatible type "str"; expected "int". Django-stubs claims only int is supported (https://github.com/typeddjango/django-stubs/blob/90ed7f332def8922d4c72c046794a446b0e4cbc2/django-stubs/db/models/expressions.pyi#L38) but in PostgreSQL the macaddr type supports bitwise operations with IP and MAC addresses expressed as strings.

sobolevn commented 4 years ago

@dfarley1 what about other databases?

dfarley1 commented 4 years ago

I haven't tried any other DBs, PostgreSQL is all I use and the netfields package doesn't support any other DBs. However, the bitwise operators are universal to SQL so I would imagine you can "try" to do bitwise operations on any value that you can put in an SQL query.