Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pycharm: Expected type 'Integral', got 'str' instead

Tags:

python

pycharm

I just installed PyCharm 3.4 and get some new warnings. Not just here but in many places. Code is fine of course. Can someone translate what PyCharm trying to tell me and how to silence this messages?

screenshotmore...

like image 336
avalanchy Avatar asked May 30 '14 19:05

avalanchy


2 Answers

Based on the 'more...' screenshot, it looks like Pycharm might be interpreting the map() as though the two terms around the comma are both part of the lambda, i.e. the lambda just returns a 2-tuple instead of treating it as two parameters to the map() function.

Things to try:

  • Add parentheses inside the map()
  • look for redefinitions of the map() builtin itself that might be confusing Pycharm

EDIT

You inspired me to go learn more about Python and Pycharm. :)

It looks like Pycharm is happier with using a list comprehension than with map(). Using this sample data:

data = {
    'data': {
        'children': [
            {'data': {'url': 'http://1.com/', }, },
            {'data': {'url': 'http://2.com/', }, },
        ]
    },
}

if you write the code like you did, then you get the error:

items = map(lambda children: children['data'], data['data']['children'])
for item in items:
    print item['url']  # Pycharm shows warning on 'url'

But if you use a list comprehension instead, then Pycharm is happy:

items = [x['data'] for x in data['data']['children']]
for item in items:
    print item['url']  # No warning from Pycharm

And the output is the same for both.

ISTR reading that list comprehensions are preferred over map() nowadays anyway, so maybe Pycharm is nudging us in that direction?

like image 65
David Pope Avatar answered Nov 16 '22 12:11

David Pope


You can also get rid of this notification if you get the value from the dictionary with get(). This should work: fresh_urls = {item.get('url') for item in items}

like image 4
t_io Avatar answered Nov 16 '22 12:11

t_io