Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/libcst/_position.py: 71%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

28 statements  

1# Copyright (c) Meta Platforms, Inc. and affiliates. 

2# 

3# This source code is licensed under the MIT license found in the 

4# LICENSE file in the root directory of this source tree. 

5 

6 

7""" 

8Data structures used for storing position information. 

9 

10These are publicly exported by metadata, but their implementation lives outside of 

11metadata, because they're used internally by the codegen logic, which computes position 

12locations. 

13""" 

14 

15from dataclasses import dataclass 

16from typing import cast, overload, Tuple, Union 

17 

18from libcst._add_slots import add_slots 

19 

20_CodePositionT = Union[Tuple[int, int], "CodePosition"] 

21 

22 

23@add_slots 

24@dataclass(frozen=True) 

25class CodePosition: 

26 #: Line numbers are 1-indexed. 

27 line: int 

28 #: Column numbers are 0-indexed. 

29 column: int 

30 

31 

32@add_slots 

33@dataclass(frozen=True) 

34# pyre-fixme[13]: Attribute `end` is never initialized. 

35# pyre-fixme[13]: Attribute `start` is never initialized. 

36class CodeRange: 

37 #: Starting position of a node (inclusive). 

38 start: CodePosition 

39 #: Ending position of a node (exclusive). 

40 end: CodePosition 

41 

42 @overload 

43 def __init__(self, start: CodePosition, end: CodePosition) -> None: ... 

44 

45 @overload 

46 def __init__(self, start: Tuple[int, int], end: Tuple[int, int]) -> None: ... 

47 

48 def __init__(self, start: _CodePositionT, end: _CodePositionT) -> None: 

49 if isinstance(start, tuple) and isinstance(end, tuple): 

50 object.__setattr__(self, "start", CodePosition(start[0], start[1])) 

51 object.__setattr__(self, "end", CodePosition(end[0], end[1])) 

52 else: 

53 start = cast(CodePosition, start) 

54 end = cast(CodePosition, end) 

55 object.__setattr__(self, "start", start) 

56 object.__setattr__(self, "end", end)