dfunckt / django-rules

Awesome Django authorization, without the database

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support for PEP 484 annotations

blueyed opened this issue · comments

Using function annotations in rules will cause this error (Python 3.5):

ValueError: Function has keyword-only arguments or annotations, use getfullargspec() API which can support them

File "…/app/rules.py", line 3, in <module>
  from .predicates import (
File "…/app/predicates.py", line 13, in <module>
  def is_superuser(user: User):
File "…/django-rules/rules/predicates.py", line 246, in predicate
  return inner(fn)
File "…/django-rules/rules/predicates.py", line 241, in inner
  p = Predicate(fn, name, **options)
File "…/django-rules/rules/predicates.py", line 67, in __init__
  argspec = inspect.getargspec(fn)
File "/usr/lib64/python3.5/inspect.py", line 1045, in getargspec
  raise ValueError("Function has keyword-only arguments or annotations"

Ref: https://docs.python.org/3/library/inspect.html#inspect.getfullargspec

I tried replacing getargspec with getfullargspec and it works in Python 3 but not in Python 2. Django 1.11 LTS supports both Python 2 & 3 and will be supported until early 2020 so I assume both versions of Python will be supported by rules as well?

Yeah, I'm really not sure what's the way forward here. That sort of incompatibility is what stopped me from adding this back when I looked into it -- I couldn't find a way to support both Python versions in a straightforward way. I don't think dropping support for Python 2 and becoming 3-only makes sense yet, even though this library was one of the first to support both versions from day one, so it clearly cares about Python 3 :)

In general I'm in favour of keeping support for as many Python versions as possible -- the code is simple enough and there's not much cruft added in order to support the different versions and it makes no sense to put artificial barriers because one version is eg. LTS but the other is not.

I don't remember the exact sort of incompatibility, but could we maybe do:

try:
  from blah import getfullargspec
  # implementation using getfullargspec
except ImportError:
  # the current implementation

If that can done, we don't even need to ship this with rules-2.0, it could be included in the next minor version.

I'll try to fix a PR today for both Python 2 & 3 :)

Fixed in 211fcd4, shipped with rules-1.4. Thank you!