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

AttributeError: 'int' object has no attribute 'isnumeric' #117

Closed
brianmay opened this issue Apr 10, 2015 · 6 comments
Closed

AttributeError: 'int' object has no attribute 'isnumeric' #117

brianmay opened this issue Apr 10, 2015 · 6 comments

Comments

@brianmay
Copy link

Hello,

If the value to _as_pk is already int, _as_pk fails:

Traceback (most recent call last):
  File "/home/brian/tree/django/karaage/karaage/karaage/tests/machines/test_forms.py", line 49, in test_valid_data
    self.assertEqual(form.is_valid(), True, form.errors.items())
  File "/usr/lib/python3/dist-packages/django/forms/forms.py", line 162, in is_valid
    return self.is_bound and not bool(self.errors)
  File "/usr/lib/python3/dist-packages/django/forms/forms.py", line 154, in errors
    self.full_clean()
  File "/usr/lib/python3/dist-packages/django/forms/forms.py", line 353, in full_clean
    self._clean_fields()
  File "/usr/lib/python3/dist-packages/django/forms/forms.py", line 362, in _clean_fields
    value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name))
  File "/usr/lib/python3/dist-packages/ajax_select/fields.py", line 111, in value_from_datadict
    return _as_pk(data.get(name, None))
  File "/usr/lib/python3/dist-packages/ajax_select/fields.py", line 29, in _as_pk
    if got.isnumeric():
AttributeError: 'int' object has no attribute 'isnumeric'
@brianmay
Copy link
Author

I suggest something like the following patch to fix this:

Index: django-ajax-selects/ajax_select/fields.py
===================================================================
--- django-ajax-selects.orig/ajax_select/fields.py      2015-04-07 01:41:13.000000000 +1000
+++ django-ajax-selects/ajax_select/fields.py   2015-04-10 13:19:52.290799463 +1000
@@ -26,6 +26,10 @@

 def _as_pk(got):
     # a unicode method that checks for integers
+    if isinstance(got, int):
+        return got
+    if IS_PYTHON2 and isinstance(got, long):
+        return got
     if got.isnumeric():
         if IS_PYTHON2:
             return long(got)

@crucialfelix
Copy link
Owner

isnumeric is a method of Unicode. So we should check that first.

I've gotten unit tests and Travis, Tox set up so soon these pull requests
can be vetted a lot easier. With all the Django versions and python
versions it's become difficult to maintain.
On Fri, Apr 10, 2015 at 5:21 AM Brian May notifications@github.com wrote:

I suggest something like the following patch to fix this:

Index: django-ajax-selects/ajax_select/fields.py===================================================================--- django-ajax-selects.orig/ajax_select/fields.py 2015-04-07 01:41:13.000000000 +1000+++ django-ajax-selects/ajax_select/fields.py 2015-04-10 13:19:52.290799463 +1000@@ -26,6 +26,10 @@

def _as_pk(got):
# a unicode method that checks for integers+ if isinstance(got, int):+ return got+ if IS_PYTHON2 and isinstance(got, long):+ return got
if got.isnumeric():
if IS_PYTHON2:
return long(got)


Reply to this email directly or view it on GitHub
#117 (comment)
.

@crucialfelix
Copy link
Owner

basic travis and tox are now added. I will implement tests when I get a
chance.

I think that maybe your test (the one that showed this error) is supplying
an int, but coming in from the request it is always a unicode.

in any case the correct way is to get the model field and use its
topython()

https://docs.djangoproject.com/en/1.8/howto/custom-model-fields/#converting-values-to-python-objects

but I don't see any reason why the form field or widget should be doing
this anyway. it was sent as a pull request and I maybe should not have
accepted it.

On Fri, Apr 10, 2015 at 8:16 AM felix crucialfelix@gmail.com wrote:

isnumeric is a method of Unicode. So we should check that first.

I've gotten unit tests and Travis, Tox set up so soon these pull requests
can be vetted a lot easier. With all the Django versions and python
versions it's become difficult to maintain.
On Fri, Apr 10, 2015 at 5:21 AM Brian May notifications@github.com
wrote:

I suggest something like the following patch to fix this:

Index: django-ajax-selects/ajax_select/fields.py===================================================================--- django-ajax-selects.orig/ajax_select/fields.py 2015-04-07 01:41:13.000000000 +1000+++ django-ajax-selects/ajax_select/fields.py 2015-04-10 13:19:52.290799463 +1000@@ -26,6 +26,10 @@

def _as_pk(got):
# a unicode method that checks for integers+ if isinstance(got, int):+ return got+ if IS_PYTHON2 and isinstance(got, long):+ return got
if got.isnumeric():
if IS_PYTHON2:
return long(got)


Reply to this email directly or view it on GitHub
#117 (comment)
.

@brianmay
Copy link
Author

Thanks for your prompt fix.

Yes, I was wondering about my code too. I couldn't see the problem yesterday. Oh wait, I see the issue now, will fix it.

hwkns added a commit to hwkns/django-ajax-selects that referenced this issue Oct 28, 2015
crucialfelix added a commit that referenced this issue Oct 28, 2015
Get rid of terrible `_as_pk` function (fixes #117, #120, and #135)
@gwenau
Copy link

gwenau commented Feb 25, 2019

isnumeric() works in Python 3. It does not seem to work in Python2.

@crucialfelix
Copy link
Owner

It's a method of unicode, but not of str:

>>> u"2".isnumeric()
True
>>> "2".isnumeric()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'isnumeric'

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

3 participants