Skip to content

Commit

Permalink
fix: change IO to Union[TextIO, BinaryIO]
Browse files Browse the repository at this point in the history
This is more accurate and eliminates type errors.
  • Loading branch information
aucampia committed Mar 12, 2023
1 parent 77ef8cd commit eebb27f
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions rdflib/plugins/sparql/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

import re
import sys
from typing import IO, Any
from typing import Any, BinaryIO
from typing import Optional as OptionalType
from typing import Tuple, Union
from typing import TextIO, Tuple, Union

from pyparsing import CaselessKeyword as Keyword # watch out :)
from pyparsing import (
Expand Down Expand Up @@ -1531,24 +1531,22 @@ def expand(m: re.Match) -> str:
return expandUnicodeEscapes_re.sub(expand, q)


def parseQuery(q: Union[str, bytes, IO]) -> ParseResults:
def parseQuery(q: Union[str, bytes, TextIO, BinaryIO]) -> ParseResults:
if hasattr(q, "read"):
q = q.read()
if isinstance(q, bytes):
q = q.decode("utf-8")

# type error: Argument 1 to "expandUnicodeEscapes" has incompatible type "Union[str, IO[Any]]"; expected "str"
q = expandUnicodeEscapes(q) # type: ignore[arg-type]
q = expandUnicodeEscapes(q)
return Query.parseString(q, parseAll=True)


def parseUpdate(q: Union[str, bytes, IO]):
def parseUpdate(q: Union[str, bytes, TextIO, BinaryIO]):
if hasattr(q, "read"):
q = q.read()

if isinstance(q, bytes):
q = q.decode("utf-8")

# type error: Argument 1 to "expandUnicodeEscapes" has incompatible type "Union[str, IO[Any]]"; expected "str"
q = expandUnicodeEscapes(q) # type: ignore[arg-type]
q = expandUnicodeEscapes(q)
return UpdateUnit.parseString(q, parseAll=True)[0]

0 comments on commit eebb27f

Please sign in to comment.