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

Move types into source, drop Python 3.4 #96

Merged
merged 2 commits into from Jan 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions .travis.yml
Expand Up @@ -3,19 +3,26 @@ arch:
- amd64
- ppc64le
python:
- "3.4"
- "3.5"
- "3.6"
- "3.7"
- "3.8"
- "3.9"
- "pypy3"
# Disable unsuported version pypy for ppc64le
# Disable unsupported version pypy for ppc64le
jobs:
exclude:
- arch: ppc64le
python: pypy3

matrix:
include:
- python: 3.9
install:
- pip install mypy
script:
- mypy --strict idna/

install:
- pip install .
script:
Expand Down
1 change: 0 additions & 1 deletion MANIFEST.in
@@ -1,7 +1,6 @@
include *.rst
include LICENSE.md
include idna/py.typed
recursive-include idna *.pyi
recursive-include tools *
recursive-exclude tools *.pyc
recursive-include tests *
Expand Down
2 changes: 1 addition & 1 deletion idna/__init__.py
Expand Up @@ -12,14 +12,14 @@
check_nfc,
decode,
encode,
intranges_contain,
ulabel,
uts46_remap,
valid_contextj,
valid_contexto,
valid_label_length,
valid_string_length,
)
from .intranges import intranges_contain

__all__ = [
"IDNABidiError",
Expand Down
31 changes: 19 additions & 12 deletions idna/codec.py
@@ -1,23 +1,24 @@
from .core import encode, decode, alabel, ulabel, IDNAError
import codecs
import re
from typing import Tuple, Optional

_unicode_dots_re = re.compile('[\u002e\u3002\uff0e\uff61]')

class Codec(codecs.Codec):

def encode(self, data, errors='strict'):

# type: (str, str) -> Tuple[bytes, int]
if errors != 'strict':
raise IDNAError('Unsupported error handling \"{}\"'.format(errors))

if not data:
return "", 0
return b"", 0

return encode(data), len(data)

def decode(self, data, errors='strict'):

# type: (bytes, str) -> Tuple[str, int]
if errors != 'strict':
raise IDNAError('Unsupported error handling \"{}\"'.format(errors))

Expand All @@ -27,12 +28,13 @@ def decode(self, data, errors='strict'):
return decode(data), len(data)

class IncrementalEncoder(codecs.BufferedIncrementalEncoder):
def _buffer_encode(self, data, errors, final):
def _buffer_encode(self, data, errors, final): # type: ignore
# type: (str, str, bool) -> Tuple[str, int]
if errors != 'strict':
raise IDNAError('Unsupported error handling \"{}\"'.format(errors))

if not data:
return ('', 0)
return "", 0

labels = _unicode_dots_re.split(data)
trailing_dot = ''
Expand All @@ -55,12 +57,13 @@ def _buffer_encode(self, data, errors, final):
size += len(label)

# Join with U+002E
result = '.'.join(result) + trailing_dot
result_str = '.'.join(result) + trailing_dot # type: ignore
size += len(trailing_dot)
return (result, size)
return result_str, size
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again possibly not the correct moment to modify this within the current pull request, but I wonder if it's possible to simplify the logic around the returned string value and size variable. In particular: is size always equal to len(result_str)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole file needs a lot of work, I mentioned it in initial PR message that I'm almost certain the idna.codec module is not functional. Would recommend solving all these problems in a different PR.


class IncrementalDecoder(codecs.BufferedIncrementalDecoder):
def _buffer_decode(self, data, errors, final):
def _buffer_decode(self, data, errors, final): # type: ignore
# type: (str, str, bool) -> Tuple[str, int]
if errors != 'strict':
raise IDNAError('Unsupported error handling \"{}\"'.format(errors))

Expand All @@ -87,22 +90,26 @@ def _buffer_decode(self, data, errors, final):
size += 1
size += len(label)

result = '.'.join(result) + trailing_dot
result_str = '.'.join(result) + trailing_dot
size += len(trailing_dot)
return (result, size)
return (result_str, size)


class StreamWriter(Codec, codecs.StreamWriter):
pass


class StreamReader(Codec, codecs.StreamReader):
pass


def getregentry():
# type: () -> codecs.CodecInfo
# Compatibility as a search_function for codecs.register()
return codecs.CodecInfo(
name='idna',
encode=Codec().encode,
decode=Codec().decode,
encode=Codec().encode, # type: ignore
decode=Codec().decode, # type: ignore
incrementalencoder=IncrementalEncoder,
incrementaldecoder=IncrementalDecoder,
streamwriter=StreamWriter,
Expand Down
27 changes: 0 additions & 27 deletions idna/codec.pyi

This file was deleted.

4 changes: 4 additions & 0 deletions idna/compat.py
@@ -1,12 +1,16 @@
from .core import *
from .codec import *
from typing import Any, Union

def ToASCII(label):
# type: (str) -> bytes
return encode(label)

def ToUnicode(label):
# type: (Union[bytes, bytearray]) -> str
return decode(label)

def nameprep(s):
# type: (Any) -> None
raise NotImplementedError('IDNA 2008 does not utilise nameprep protocol')

5 changes: 0 additions & 5 deletions idna/compat.pyi

This file was deleted.