How to use the typesystem.base.Position function in typesystem

To help you get started, we’ve selected a few typesystem examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github encode / typesystem / tests / tokenize / test_validate_yaml.py View on Github external
index=["b"],
            start_position=Position(line_no=2, column_no=4, char_index=10),
            end_position=Position(line_no=2, column_no=6, char_index=12),
        )
    ]

    text = "a: 123"
    with pytest.raises(ValidationError) as exc_info:
        validate_yaml(text, validator=Validator)
    exc = exc_info.value
    assert exc.messages() == [
        Message(
            text="The field 'b' is required.",
            code="required",
            index=["b"],
            start_position=Position(line_no=1, column_no=1, char_index=0),
            end_position=Position(line_no=1, column_no=6, char_index=5),
        )
github encode / typesystem / tests / tokenize / test_validate_json.py View on Github external
assert value == {"a": 123, "b": 456}

    validator = Object(properties=Integer())
    text = '{\n    "a": "123",\n    "b": "abc"}'

    with pytest.raises(ValidationError) as exc_info:
        validate_json(text, validator=validator)
    exc = exc_info.value

    assert exc.messages() == [
        Message(
            text="Must be a number.",
            code="type",
            index=["b"],
            start_position=Position(line_no=3, column_no=10, char_index=27),
            end_position=Position(line_no=3, column_no=14, char_index=31),
        )
    ]
    assert (
        repr(exc.messages()[0])
        == "Message(text='Must be a number.', code='type', index=['b'], start_position=Position(line_no=3, column_no=10, char_index=27), end_position=Position(line_no=3, column_no=14, char_index=31))"
    )

    class Validator(Schema):
        a = Integer()
        b = Integer()

    text = '{\n    "a": "123",\n    "b": "abc"}'
    with pytest.raises(ValidationError) as exc_info:
        validate_json(text, validator=Validator)
    exc = exc_info.value
    assert exc.messages() == [
github encode / typesystem / tests / tokenize / test_validate_json.py View on Github external
index=["b"],
            start_position=Position(line_no=3, column_no=10, char_index=27),
            end_position=Position(line_no=3, column_no=14, char_index=31),
        )
    ]

    text = '{"a": "123"}'
    with pytest.raises(ValidationError) as exc_info:
        validate_json(text, validator=Validator)
    exc = exc_info.value
    assert exc.messages() == [
        Message(
            text="The field 'b' is required.",
            code="required",
            index=["b"],
            start_position=Position(line_no=1, column_no=1, char_index=0),
            end_position=Position(line_no=1, column_no=12, char_index=11),
        )
github encode / typesystem / typesystem / tokenize / tokens.py View on Github external
def _get_position(self, index: int) -> Position:
        content = self._content[: index + 1]
        lines = content.splitlines()
        line_no = max(len(lines), 1)
        column_no = 1 if not lines else max(len(lines[-1]), 1)
        return Position(line_no, column_no, index)
github encode / typesystem / typesystem / tokenize / tokenize_yaml.py View on Github external
def _get_position(content: str, index: int) -> Position:
    return Position(
        line_no=content.count("\n", 0, index) + 1,
        column_no=index - content.rfind("\n", 0, index),
        char_index=index,
    )
github encode / typesystem / typesystem / tokenize / tokenize_yaml.py View on Github external
def tokenize_yaml(content: typing.Union[str, bytes]) -> Token:
    assert yaml is not None, "'pyyaml' must be installed."

    if isinstance(content, bytes):
        str_content = content.decode("utf-8", "ignore")
    else:
        str_content = content

    if not str_content.strip():
        # Handle the empty string case explicitly for clear error messaging.
        position = Position(column_no=1, line_no=1, char_index=0)
        raise ParseError(text="No content.", code="no_content", position=position)

    class CustomSafeLoader(SafeLoader):
        pass

    def construct_mapping(loader: "yaml.Loader", node: "yaml.Node") -> DictToken:
        start = node.start_mark.index
        end = node.end_mark.index
        mapping = loader.construct_mapping(node)
        return DictToken(mapping, start, end - 1, content=str_content)

    def construct_sequence(loader: "yaml.Loader", node: "yaml.Node") -> ListToken:
        start = node.start_mark.index
        end = node.end_mark.index
        value = loader.construct_sequence(node)
        return ListToken(value, start, end - 1, content=str_content)
github encode / typesystem / typesystem / base.py View on Github external
def __eq__(self, other: typing.Any) -> bool:
        return (
            isinstance(other, Position)
            and self.line_no == other.line_no
            and self.column_no == other.column_no
            and self.char_index == other.char_index
        )
github encode / typesystem / typesystem / tokenize / tokenize_json.py View on Github external
def tokenize_json(content: typing.Union[str, bytes]) -> Token:
    if isinstance(content, bytes):
        content = content.decode("utf-8", "ignore")

    if not content.strip():
        # Handle the empty string case explicitly for clear error messaging.
        position = Position(column_no=1, line_no=1, char_index=0)
        raise ParseError(text="No content.", code="no_content", position=position)

    decoder = _TokenizingDecoder(content=content)
    try:
        return decoder.decode(content)
    except JSONDecodeError as exc:
        # Handle cases that result in a JSON parse error.
        position = Position(column_no=exc.colno, line_no=exc.lineno, char_index=exc.pos)
        raise ParseError(text=exc.msg + ".", code="parse_error", position=position)