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

29 statements  

« prev     ^ index     » next       coverage.py v7.3.1, created at 2023-09-25 06:43 +0000

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 

46 @overload 

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

48 ... 

49 

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

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

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

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

54 else: 

55 start = cast(CodePosition, start) 

56 end = cast(CodePosition, end) 

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

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