question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Type hints and Response.selector

See original GitHub issue

Python 3.5 introduced Type Hints: https://docs.python.org/3/library/typing.html

I’m using them with Scrapy’s response object, but it seems that however you’ve declared selectors in scrapy it makes PyCharm unable to realise there’s a selector method/attribute:

from scrapy.http import Response

def my_function(response: Response):
    response.selector.remove_namespaces()

For which PyCharm reports: “Unresolved attribute reference ‘selector’ for class ‘Response’” Obviously the selector works, but it’s not being exposed sufficiently well for the IDE to be able to spot it which obviates the point of type hints.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
kmikecommented, Feb 13, 2019

probably you need

def my_function(response: Union[Response, TextResponse]):
    assert isinatance(response, TextResponse)
    response.selector.remove_namespaces()

or just

from scrapy.http import TextResponse

def my_function(response: TextResponse):
    response.selector.remove_namespaces()
1reaction
kmikecommented, Feb 13, 2019

Type hints are correct in pointing this out - Response doesn’t have .selector attribute, only TextResponse does. Scrapy selects Response subclass based on server response body and headers; for binary data it’d be just Response (=> no parsed HTML tree), while for HTML it’d be HtmlResponse, which is a Response subclass.

Read more comments on GitHub >

github_iconTop Results From Across the Web

typing — Support for type hints — Python 3.11.1 documentation
This module provides runtime support for type hints. The most fundamental support consists of the types Any , Union , Callable , TypeVar...
Read more >
Advanced static type hints in Python | Lab Digital
A writeup of advanced python static type annotation techniques (protocols, aliasing, generics) and its implications (automated property ...
Read more >
Putting Type Hints to Work - YouTube
Python 3.5 and Python 3.6 embraced optional typing as part of the Python language. WAT? TYPING IN PYTHON?This webinar introduces the subject ...
Read more >
Python Types Intro - FastAPI
These "type hints" are a special syntax that allow declaring the type of a variable. By declaring types for your variables, editors and...
Read more >
Type hints cheat sheet - mypy 0.991 documentation
Technically many of the type annotations shown below are redundant, since mypy can usually infer the type of a variable from its value....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found