avalanchy
avalanchy

Reputation: 851

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

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?

screenshot more...

Upvotes: 14

Views: 34608

Answers (2)

t_io
t_io

Reputation: 2032

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}

Upvotes: 5

David Pope
David Pope

Reputation: 6587

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?

Upvotes: 9

Related Questions