Skip to content

Commit 3514a7d

Browse files
Use Optional, CR
1 parent 4ef513d commit 3514a7d

File tree

7 files changed

+36
-52
lines changed

7 files changed

+36
-52
lines changed

bpython/autocomplete.py

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class AutocompleteModes(Enum):
6666
FUZZY = "fuzzy"
6767

6868
@classmethod
69-
def from_string(cls, value: str) -> Union[Any, None]:
69+
def from_string(cls, value: str) -> Optional[Any]:
7070
if value.upper() in cls.__members__:
7171
return cls.__members__[value.upper()]
7272
return None
@@ -209,7 +209,7 @@ def method_match_substring(word: str, size: int, text: str) -> bool:
209209
return text in word
210210

211211

212-
def method_match_fuzzy(word: str, size: int, text: str) -> Union[Match, None]:
212+
def method_match_fuzzy(word: str, size: int, text: str) -> Optional[Match]:
213213
s = r".*%s.*" % ".*".join(list(text))
214214
return re.search(s, word)
215215

@@ -236,7 +236,7 @@ def __init__(
236236
@abc.abstractmethod
237237
def matches(
238238
self, cursor_offset: int, line: str, **kwargs: Any
239-
) -> Union[Set[str], None]:
239+
) -> Optional[Set[str]]:
240240
"""Returns a list of possible matches given a line and cursor, or None
241241
if this completion type isn't applicable.
242242
@@ -255,7 +255,7 @@ def matches(
255255
raise NotImplementedError
256256

257257
@abc.abstractmethod
258-
def locate(self, cursor_offset: int, line: str) -> Union[LinePart, None]:
258+
def locate(self, cursor_offset: int, line: str) -> Optional[LinePart]:
259259
"""Returns a Linepart namedtuple instance or None given cursor and line
260260
261261
A Linepart namedtuple contains a start, stop, and word. None is
@@ -299,7 +299,7 @@ def __init__(
299299

300300
super().__init__(True, mode)
301301

302-
def locate(self, cursor_offset: int, line: str) -> Union[LinePart, None]:
302+
def locate(self, cursor_offset: int, line: str) -> Optional[LinePart]:
303303
for completer in self._completers:
304304
return_value = completer.locate(cursor_offset, line)
305305
if return_value is not None:
@@ -311,7 +311,7 @@ def format(self, word: str) -> str:
311311

312312
def matches(
313313
self, cursor_offset: int, line: str, **kwargs: Any
314-
) -> Union[None, Set]:
314+
) -> Optional[Set]:
315315
return_value = None
316316
all_matches = set()
317317
for completer in self._completers:
@@ -336,10 +336,10 @@ def __init__(
336336

337337
def matches(
338338
self, cursor_offset: int, line: str, **kwargs: Any
339-
) -> Union[None, Set]:
339+
) -> Optional[Set]:
340340
return self.module_gatherer.complete(cursor_offset, line)
341341

342-
def locate(self, cursor_offset: int, line: str) -> Union[LinePart, None]:
342+
def locate(self, cursor_offset: int, line: str) -> Optional[LinePart]:
343343
return lineparts.current_word(cursor_offset, line)
344344

345345
def format(self, word: str) -> str:
@@ -355,7 +355,7 @@ def safe_glob(self, pathname: str) -> Iterator[str]:
355355

356356
def matches(
357357
self, cursor_offset: int, line: str, **kwargs: Any
358-
) -> Union[None, Set]:
358+
) -> Optional[Set]:
359359
cs = lineparts.current_string(cursor_offset, line)
360360
if cs is None:
361361
return None
@@ -370,7 +370,7 @@ def matches(
370370
matches.add(filename)
371371
return matches
372372

373-
def locate(self, cursor_offset: int, line: str) -> Union[LinePart, None]:
373+
def locate(self, cursor_offset: int, line: str) -> Optional[LinePart]:
374374
return lineparts.current_string(cursor_offset, line)
375375

376376
def format(self, filename: str) -> str:
@@ -387,7 +387,7 @@ class AttrCompletion(BaseCompletionType):
387387

388388
def matches(
389389
self, cursor_offset: int, line: str, **kwargs: Any
390-
) -> Union[None, Set]:
390+
) -> Optional[Set]:
391391
if "locals_" not in kwargs:
392392
return None
393393
locals_ = cast(Dict[str, Any], kwargs["locals_"])
@@ -417,7 +417,7 @@ def matches(
417417
if few_enough_underscores(r.word.split(".")[-1], m.split(".")[-1])
418418
}
419419

420-
def locate(self, cursor_offset: int, line: str) -> Union[LinePart, None]:
420+
def locate(self, cursor_offset: int, line: str) -> Optional[LinePart]:
421421
return lineparts.current_dotted_attribute(cursor_offset, line)
422422

423423
def format(self, word: str) -> str:
@@ -472,7 +472,7 @@ def list_attributes(self, obj: Any) -> List[str]:
472472
class DictKeyCompletion(BaseCompletionType):
473473
def matches(
474474
self, cursor_offset: int, line: str, **kwargs: Any
475-
) -> Union[None, Set]:
475+
) -> Optional[Set]:
476476
if "locals_" not in kwargs:
477477
return None
478478
locals_ = kwargs["locals_"]
@@ -495,7 +495,7 @@ def matches(
495495
else:
496496
return None
497497

498-
def locate(self, cursor_offset: int, line: str) -> Union[LinePart, None]:
498+
def locate(self, cursor_offset: int, line: str) -> Optional[LinePart]:
499499
return lineparts.current_dict_key(cursor_offset, line)
500500

501501
def format(self, match: str) -> str:
@@ -505,7 +505,7 @@ def format(self, match: str) -> str:
505505
class MagicMethodCompletion(BaseCompletionType):
506506
def matches(
507507
self, cursor_offset: int, line: str, **kwargs: Any
508-
) -> Union[None, Set]:
508+
) -> Optional[Set]:
509509
if "current_block" not in kwargs:
510510
return None
511511
current_block = kwargs["current_block"]
@@ -517,14 +517,14 @@ def matches(
517517
return None
518518
return {name for name in MAGIC_METHODS if name.startswith(r.word)}
519519

520-
def locate(self, cursor_offset: int, line: str) -> Union[LinePart, None]:
520+
def locate(self, cursor_offset: int, line: str) -> Optional[LinePart]:
521521
return lineparts.current_method_definition_name(cursor_offset, line)
522522

523523

524524
class GlobalCompletion(BaseCompletionType):
525525
def matches(
526526
self, cursor_offset: int, line: str, **kwargs: Any
527-
) -> Union[None, Set]:
527+
) -> Optional[Set]:
528528
"""Compute matches when text is a simple name.
529529
Return a list of all keywords, built-in functions and names currently
530530
defined in self.namespace that match.
@@ -554,14 +554,14 @@ def matches(
554554
matches.add(_callable_postfix(val, word))
555555
return matches if matches else None
556556

557-
def locate(self, cursor_offset: int, line: str) -> Union[LinePart, None]:
557+
def locate(self, cursor_offset: int, line: str) -> Optional[LinePart]:
558558
return lineparts.current_single_word(cursor_offset, line)
559559

560560

561561
class ParameterNameCompletion(BaseCompletionType):
562562
def matches(
563563
self, cursor_offset: int, line: str, **kwargs: Any
564-
) -> Union[None, Set]:
564+
) -> Optional[Set]:
565565
if "argspec" not in kwargs:
566566
return None
567567
argspec = kwargs["argspec"]
@@ -582,18 +582,18 @@ def matches(
582582
)
583583
return matches if matches else None
584584

585-
def locate(self, cursor_offset: int, line: str) -> Union[LinePart, None]:
585+
def locate(self, cursor_offset: int, line: str) -> Optional[LinePart]:
586586
return lineparts.current_word(cursor_offset, line)
587587

588588

589589
class ExpressionAttributeCompletion(AttrCompletion):
590590
# could replace attr completion as a more general case with some work
591-
def locate(self, cursor_offset: int, line: str) -> Union[LinePart, None]:
591+
def locate(self, cursor_offset: int, line: str) -> Optional[LinePart]:
592592
return lineparts.current_expression_attribute(cursor_offset, line)
593593

594594
def matches(
595595
self, cursor_offset: int, line: str, **kwargs: Any
596-
) -> Union[None, Set]:
596+
) -> Optional[Set]:
597597
if "locals_" not in kwargs:
598598
return None
599599
locals_ = kwargs["locals_"]
@@ -621,23 +621,21 @@ def matches(
621621
class MultilineJediCompletion(BaseCompletionType): # type: ignore [no-redef]
622622
def matches(
623623
self, cursor_offset: int, line: str, **kwargs: Any
624-
) -> Union[None, Set]:
624+
) -> Optional[Set]:
625625
return None
626626

627-
def locate(
628-
self, cursor_offset: int, line: str
629-
) -> Union[LinePart, None]:
627+
def locate(self, cursor_offset: int, line: str) -> Optional[LinePart]:
630628
return None
631629

632630

633631
else:
634632

635633
class JediCompletion(BaseCompletionType):
636-
_orig_start: Union[int, None]
634+
_orig_start: Optional[int]
637635

638636
def matches(
639637
self, cursor_offset: int, line: str, **kwargs: Any
640-
) -> Union[None, Set]:
638+
) -> Optional[Set]:
641639
if "history" not in kwargs:
642640
return None
643641
history = kwargs["history"]
@@ -687,7 +685,7 @@ def locate(self, cursor_offset: int, line: str) -> LinePart:
687685
class MultilineJediCompletion(JediCompletion): # type: ignore [no-redef]
688686
def matches(
689687
self, cursor_offset: int, line: str, **kwargs: Any
690-
) -> Union[None, Set]:
688+
) -> Optional[Set]:
691689
if "current_block" not in kwargs or "history" not in kwargs:
692690
return None
693691
current_block = kwargs["current_block"]

bpython/cli.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
import sys
5252
import time
5353
from typing import Iterator, NoReturn, List
54-
from typing_extensions import Literal
5554
import unicodedata
5655
from dataclasses import dataclass
5756

@@ -145,7 +144,7 @@ def writelines(self, l) -> None:
145144
for s in l:
146145
self.write(s)
147146

148-
def isatty(self) -> Literal[True]:
147+
def isatty(self) -> bool:
149148
# some third party (amongst them mercurial) depend on this
150149
return True
151150

@@ -176,7 +175,7 @@ def write(self, value) -> NoReturn:
176175
# others, so here's a hack to keep them happy
177176
raise OSError(errno.EBADF, "sys.stdin is read-only")
178177

179-
def isatty(self) -> Literal[True]:
178+
def isatty(self) -> bool:
180179
return True
181180

182181
def readline(self, size=-1):

bpython/curtsies.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040

4141
class SupportsEventGeneration(Protocol):
4242
def send(
43-
self, timeout: Union[float, None]
43+
self, timeout: Optional[float]
4444
) -> Union[str, curtsies.events.Event, None]:
4545
...
4646

@@ -253,9 +253,7 @@ def curtsies_arguments(parser: argparse._ArgumentGroup) -> None:
253253

254254
def _combined_events(
255255
event_provider: "SupportsEventGeneration", paste_threshold: int
256-
) -> Generator[
257-
Union[str, curtsies.events.Event, None], Union[float, None], None
258-
]:
256+
) -> Generator[Union[str, curtsies.events.Event, None], Optional[float], None]:
259257
"""Combines consecutive keypress events into paste events."""
260258
timeout = yield "nonsense_event" # so send can be used immediately
261259
queue: collections.deque = collections.deque()

bpython/curtsiesfrontend/interaction.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import greenlet
22
import time
3-
from typing import Optional
43
from curtsies import events
54

65
from ..translations import _

bpython/curtsiesfrontend/repl.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from enum import Enum
1515

1616
from typing import Dict, Any, List, Optional, Tuple, Union, cast
17-
from typing_extensions import Literal
1817

1918
import blessings
2019
import cwcwidth
@@ -60,8 +59,6 @@
6059
)
6160
from ..translations import _
6261

63-
InputOrOutput = Union[Literal["input"], Literal["output"]]
64-
6562
logger = logging.getLogger(__name__)
6663

6764
INCONSISTENT_HISTORY_MSG = "#<---History inconsistent with output shown--->"
@@ -286,7 +283,7 @@ def _process_ps(ps, default_ps: str):
286283
if not isinstance(ps, str):
287284
return ps
288285

289-
return ps if cwcwidth.wcswidth(ps) >= 0 else default_ps
286+
return ps if cwcwidth.wcswidth(ps, None) >= 0 else default_ps
290287

291288

292289
class BaseRepl(Repl):
@@ -387,8 +384,8 @@ def __init__(
387384
# Entries are tuples, where
388385
# - the first element the line (string, not fmtsr)
389386
# - the second element is one of 2 global constants: "input" or "output"
390-
# (use LineTypeTranslator.INPUT or LineTypeTranslator.OUTPUT to avoid typing these strings)
391-
self.all_logical_lines: List[Tuple[str, InputOrOutput]] = []
387+
# (use LineType.INPUT or LineType.OUTPUT to avoid typing these strings)
388+
self.all_logical_lines: List[Tuple[str, LineType]] = []
392389

393390
# formatted version of lines in the buffer kept around so we can
394391
# unhighlight parens using self.reprint_line as called by bpython.Repl
@@ -1629,8 +1626,8 @@ def move_screen_up(current_line_start_row):
16291626
)
16301627
else: # Common case for determining cursor position
16311628
cursor_row, cursor_column = divmod(
1632-
wcswidth(self.current_cursor_line_without_suggestion.s)
1633-
- wcswidth(self.current_line)
1629+
wcswidth(self.current_cursor_line_without_suggestion.s, None)
1630+
- wcswidth(self.current_line, None)
16341631
+ wcswidth(self.current_line, max(0, self.cursor_offset))
16351632
+ self.number_of_padding_chars_on_current_cursor_line(),
16361633
width,

bpython/test/test_inspection.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
import sys
33
import unittest
44

5-
from typing import Optional
6-
from types import ModuleType
7-
85
from bpython import inspection
96
from bpython.test.fodder import encoding_ascii
107
from bpython.test.fodder import encoding_latin1

stubs/cwcwidth.pyi

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)