Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3691,6 +3691,29 @@ def test_errors(self):
class D(UserName):
pass

def test_or(self):
UserId = NewType('UserId', int)
UserName = NewType('UserName', str)

for cls in (int, UserName):
with self.subTest(cls=cls):
self.assertEqual(UserId | cls, Union[UserId, cls])
self.assertEqual(cls | UserId, Union[cls, UserId])

self.assertEqual(get_args(UserId | cls), (UserId, cls))
self.assertEqual(get_args(cls | UserId), (cls, UserId))

def test_special_attrs(self):
UserId = NewType('UserId', int)

self.assertEqual(UserId.__name__, 'UserId')
self.assertEqual(UserId.__qualname__, 'UserId')
self.assertEqual(UserId.__module__, __name__)

def test_repr(self):
UserId = NewType('UserId', int)

self.assertEqual(repr(UserId), f'{__name__}.UserId')

class NamedTupleTests(BaseTestCase):
class NestedEmployee(NamedTuple):
Expand Down
27 changes: 22 additions & 5 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1374,6 +1374,12 @@ def _no_init(self, *args, **kwargs):
if type(self)._is_protocol:
raise TypeError('Protocols cannot be instantiated')

def _callee(depth=2, default=None):
try:
return sys._getframe(depth).f_globals['__name__']
except (AttributeError, ValueError): # For platforms without _getframe()
return default


def _allow_reckless_class_checks(depth=3):
"""Allow instance and class checks for special stdlib modules.
Expand Down Expand Up @@ -2350,7 +2356,7 @@ class body be required.
TypedDict.__mro_entries__ = lambda bases: (_TypedDict,)


def NewType(name, tp):
class NewType:
"""NewType creates simple unique types with almost zero
runtime overhead. NewType(name, tp) is considered a subtype of tp
by static type checkers. At runtime, NewType(name, tp) returns
Expand All @@ -2369,12 +2375,23 @@ def name_by_id(user_id: UserId) -> str:
num = UserId(5) + 1 # type: int
"""

def new_type(x):
def __init__(self, name, tp):
self.__name__ = name
self.__qualname__ = name
self.__module__ = _callee(default='typing')
self.__supertype__ = tp

def __repr__(self):
return f'{self.__module__}.{self.__qualname__}'

def __call__(self, x):
return x

new_type.__name__ = name
new_type.__supertype__ = tp
return new_type
def __or__(self, other):
return Union[self, other]

def __ror__(self, other):
return Union[other, self]


# Python-version-specific alias (Python 2: unicode; Python 3: str)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Refactor ``typing.NewType`` from function into callable class. Patch
provided by Yurii Karabas.
18 changes: 13 additions & 5 deletions Parser/pegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ _PyPegen_byte_offset_to_character_offset(PyObject *line, Py_ssize_t col_offset)
{
const char *str = PyUnicode_AsUTF8(line);
if (!str) {
return 0;
return -1;
}
Py_ssize_t len = strlen(str);
if (col_offset > len + 1) {
Expand All @@ -411,7 +411,7 @@ _PyPegen_byte_offset_to_character_offset(PyObject *line, Py_ssize_t col_offset)
assert(col_offset >= 0);
PyObject *text = PyUnicode_DecodeUTF8(str, col_offset, "replace");
if (!text) {
return 0;
return -1;
}
Py_ssize_t size = PyUnicode_GET_LENGTH(text);
Py_DECREF(text);
Expand Down Expand Up @@ -499,9 +499,17 @@ _PyPegen_raise_error_known_location(Parser *p, PyObject *errtype,

if (p->tok->encoding != NULL) {
col_number = _PyPegen_byte_offset_to_character_offset(error_line, col_offset);
end_col_number = end_col_number > 0 ?
_PyPegen_byte_offset_to_character_offset(error_line, end_col_offset) :
end_col_number;
if (col_number < 0) {
goto error;
}
if (end_col_number > 0) {
Py_ssize_t end_col_offset = _PyPegen_byte_offset_to_character_offset(error_line, end_col_number);
if (end_col_offset < 0) {
goto error;
} else {
end_col_number = end_col_offset;
}
}
}
tmp = Py_BuildValue("(OiiNii)", p->tok->filename, lineno, col_number, error_line, end_lineno, end_col_number);
if (!tmp) {
Expand Down
10 changes: 10 additions & 0 deletions Python/traceback.c
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,17 @@ tb_displayline(PyTracebackObject* tb, PyObject *f, PyObject *filename, int linen
// Convert the utf-8 byte offset to the actual character offset so we print the right number of carets.
assert(source_line);
Py_ssize_t start_offset = _PyPegen_byte_offset_to_character_offset(source_line, start_col_byte_offset);
if (start_offset < 0) {
err = ignore_source_errors() < 0;
goto done;
}

Py_ssize_t end_offset = _PyPegen_byte_offset_to_character_offset(source_line, end_col_byte_offset);
if (end_offset < 0) {
err = ignore_source_errors() < 0;
goto done;
}

Py_ssize_t left_end_offset = -1;
Py_ssize_t right_start_offset = -1;

Expand Down