Skip to content

Commit

Permalink
Move types into source, drop Python 3.4 (#96)
Browse files Browse the repository at this point in the history
* Move types into source, drop Python 3.4
* Make number_type Optional[str]
  • Loading branch information
sethmlarson authored Jan 29, 2021
1 parent a9e4f73 commit b0ef4bf
Show file tree
Hide file tree
Showing 17 changed files with 180 additions and 150 deletions.
11 changes: 9 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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

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
Original file line number Diff line number Diff line change
@@ -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.

Loading

0 comments on commit b0ef4bf

Please sign in to comment.