Skip to content

Issues with type hints for urllib.parse.urldefrag #2477

Closed
@mr-c

Description

@mr-c
  • PY3: reveal_type(urldefrag(...)[1]) should retain the type of the input instead of Any
  • PY2: reveal_type(urldefrag(...)[1]) should not be fixed as str; should be unicode when a unicode input is provided
  • PY2: type for urldefrag should be updated to allow unicode input
from six.moves import urllib
from typing_extensions import TYPE_CHECKING

if not TYPE_CHECKING:
    def reveal_type(ignored):
        pass

_, frg = urllib.parse.urldefrag("https://github.com/python/typeshed/#typeshed")
print(type(frg))
reveal_type(frg)

_, frg2 = urllib.parse.urldefrag(u"https://github.com/python/typeshed/#typeshed")
print(type(frg2))
reveal_type(frg2)

_, frg3 = urllib.parse.urldefrag(b"https://github.com/python/typeshed/#typeshed")
print(type(frg3))
reveal_type(frg3)
$ mypy  typing-defrag-test.py                                                                                          
typing-defrag-test.py:11: error: Revealed type is 'Any'
typing-defrag-test.py:15: error: Revealed type is 'Any'
typing-defrag-test.py:19: error: Revealed type is 'Any'

$ mypy --py2  typing-defrag-test.py                                                                                    
typing-defrag-test.py:11: error: Revealed type is 'builtins.str'
typing-defrag-test.py:13: error: Argument 1 to "urldefrag" has incompatible type "unicode"; expected "str"
typing-defrag-test.py:15: error: Revealed type is 'builtins.str'
typing-defrag-test.py:19: error: Revealed type is 'builtins.str'
$ python3 typing-defrag-test.py                                                                                         
<class 'str'>
<class 'str'>
<class 'bytes'>

$ python2 typing-defrag-test.py                                                                                        
<type 'str'>
<type 'unicode'>
<type 'str'>
$ mypy --version
mypy 0.630
$ python -V
Python 2.7.15+
$ python3 -V
Python 3.6.6+

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions