Skip to content

Commit

Permalink
chore: add id_info to ImportFrom (#332)
Browse files Browse the repository at this point in the history
As title.

It seems that our `Makefile` is improperly specified and does not
regenerate `ast_generic_v1.py`. I fixed this in this PR also.

- [X] I ran `make setup && make` to update the generated code after
editing a `.atd` file (TODO: have a CI check)
- [X] I made sure we're still backward compatible with old versions of
the CLI.
For example, the Semgrep backend need to still be able to *consume* data
	  generated by Semgrep 1.50.0.
See
https://atd.readthedocs.io/en/latest/atdgen-tutorial.html#smooth-protocol-upgrades
	  Note that the types related to the semgrep-core JSON output or the
	  semgrep-core RPC do not need to be backward compatible!
  • Loading branch information
brandonspark authored Jan 15, 2025
1 parent b54e43b commit 1d13c0e
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 110 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ FILES= \
semgrep_output_$(VER).ts \
semgrep_output_$(VER).jsonschema \
semgrep_output_$(VER).proto \
ast_generic_$(VER).py \
ast_generic_$(VER)_j.ml \
ast_generic_$(VER)_j.mli \
Language.ml \
Expand Down
7 changes: 6 additions & 1 deletion ast_generic_v1.atd
Original file line number Diff line number Diff line change
Expand Up @@ -966,7 +966,7 @@ type directive = [
| ImportFrom of (
tok (* 'import'/'from' for Python, 'include' for C *)
* module_name
* (ident * alias nullable (* as name alias *)) list
* import_from_kind list
)

| ImportAs of (tok * module_name * alias nullable) (* as name *)
Expand All @@ -987,6 +987,11 @@ type directive = [
(* ... as name *)
type alias = (ident * id_info)

type import_from_kind = [
| Direct of alias
| Aliased of (ident * alias)
]

(*****************************************************************************)
(* Toplevel *)
(*****************************************************************************)
Expand Down
114 changes: 80 additions & 34 deletions ast_generic_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

# Import annotations to allow forward references
from __future__ import annotations
from dataclasses import dataclass
from dataclasses import dataclass, field
from typing import Any, Callable, Dict, List, NoReturn, Optional, Tuple, Union

import json
Expand Down Expand Up @@ -137,6 +137,19 @@ def read_nullable(x: Any) -> Any:
return read_nullable


def _atd_read_option(read_elt: Callable[[Any], Any]) \
-> Callable[[Optional[Any]], Optional[Any]]:
def read_option(x: Any) -> Any:
if x == 'None':
return None
elif isinstance(x, List) and len(x) == 2 and x[0] == 'Some':
return read_elt(x[1])
else:
_atd_bad_json('option', x)
raise AssertionError('impossible') # keep mypy happy
return read_option


def _atd_write_unit(x: Any) -> None:
if x is None:
return x
Expand Down Expand Up @@ -232,6 +245,16 @@ def write_nullable(x: Any) -> Any:
return write_nullable


def _atd_write_option(write_elt: Callable[[Any], Any]) \
-> Callable[[Optional[Any]], Optional[Any]]:
def write_option(x: Any) -> Any:
if x is None:
return 'None'
else:
return ['Some', write_elt(x)]
return write_option


############################################################################
# Public classes
############################################################################
Expand Down Expand Up @@ -2307,6 +2330,7 @@ def to_json() -> Any:
def to_json_string(self, **kw: Any) -> str:
return json.dumps(self.to_json(), **kw)


@dataclass
class LDA:
"""Original type: operator = [ ... | LDA | ... ]"""
Expand All @@ -2323,6 +2347,7 @@ def to_json() -> Any:
def to_json_string(self, **kw: Any) -> str:
return json.dumps(self.to_json(), **kw)


@dataclass
class RDA:
"""Original type: operator = [ ... | RDA | ... ]"""
Expand All @@ -2339,6 +2364,7 @@ def to_json() -> Any:
def to_json_string(self, **kw: Any) -> str:
return json.dumps(self.to_json(), **kw)


@dataclass
class LSA:
"""Original type: operator = [ ... | LSA | ... ]"""
Expand All @@ -2355,6 +2381,7 @@ def to_json() -> Any:
def to_json_string(self, **kw: Any) -> str:
return json.dumps(self.to_json(), **kw)


@dataclass
class RSA:
"""Original type: operator = [ ... | RSA | ... ]"""
Expand Down Expand Up @@ -2619,6 +2646,23 @@ def to_json_string(self, **kw: Any) -> str:
return json.dumps(self.to_json(), **kw)


@dataclass
class Cls:
"""Original type: special = [ ... | Cls | ... ]"""

@property
def kind(self) -> str:
"""Name of the class representing this variant."""
return 'Cls'

@staticmethod
def to_json() -> Any:
return 'Cls'

def to_json_string(self, **kw: Any) -> str:
return json.dumps(self.to_json(), **kw)


@dataclass
class Self:
"""Original type: special = [ ... | Self | ... ]"""
Expand Down Expand Up @@ -2917,7 +2961,7 @@ def to_json_string(self, **kw: Any) -> str:
class Special:
"""Original type: special = [ ... ]"""

value: Union[This, Super, Self, Parent, Eval, Typeof, Instanceof, Sizeof, Defined, ConcatString, EncodedString, InterpolatedElement, Spread, HashSplat, ForOf, Op, IncrDecr_, Require, OtherSpecial]
value: Union[This, Super, Cls, Self, Parent, Eval, Typeof, Instanceof, Sizeof, Defined, ConcatString, EncodedString, InterpolatedElement, Spread, HashSplat, ForOf, Op, IncrDecr_, Require, OtherSpecial]

@property
def kind(self) -> str:
Expand All @@ -2931,6 +2975,8 @@ def from_json(cls, x: Any) -> 'Special':
return cls(This())
if x == 'Super':
return cls(Super())
if x == 'Cls':
return cls(Cls())
if x == 'Self':
return cls(Self())
if x == 'Parent':
Expand Down Expand Up @@ -3196,27 +3242,6 @@ def to_json_string(self, **kw: Any) -> str:
return json.dumps(self.to_json(), **kw)


@dataclass
class IntNullableWrap:
"""Original type: _int_nullable_wrap"""

value: Tuple[Optional[int], Tok]

@classmethod
def from_json(cls, x: Any) -> 'IntNullableWrap':
return cls((lambda x: (_atd_read_nullable(_atd_read_int)(x[0]), Tok.from_json(x[1])) if isinstance(x, list) and len(x) == 2 else _atd_bad_json('array of length 2', x))(x))

def to_json(self) -> Any:
return (lambda x: [_atd_write_nullable(_atd_write_int)(x[0]), (lambda x: x.to_json())(x[1])] if isinstance(x, tuple) and len(x) == 2 else _atd_bad_python('tuple of length 2', x))(self.value)

@classmethod
def from_json_string(cls, x: str) -> 'IntNullableWrap':
return cls.from_json(json.loads(x))

def to_json_string(self, **kw: Any) -> str:
return json.dumps(self.to_json(), **kw)


@dataclass
class IntWrap:
"""Original type: _int_wrap"""
Expand Down Expand Up @@ -3343,6 +3368,27 @@ def to_json_string(self, **kw: Any) -> str:
return json.dumps(self.to_json(), **kw)


@dataclass
class XB40703a:
"""Original type: _x_b40703a"""

value: Tuple[Optional[int], Tok]

@classmethod
def from_json(cls, x: Any) -> 'XB40703a':
return cls((lambda x: (_atd_read_nullable(_atd_read_int)(x[0]), Tok.from_json(x[1])) if isinstance(x, list) and len(x) == 2 else _atd_bad_json('array of length 2', x))(x))

def to_json(self) -> Any:
return (lambda x: [_atd_write_nullable(_atd_write_int)(x[0]), (lambda x: x.to_json())(x[1])] if isinstance(x, tuple) and len(x) == 2 else _atd_bad_python('tuple of length 2', x))(self.value)

@classmethod
def from_json_string(cls, x: str) -> 'XB40703a':
return cls.from_json(json.loads(x))

def to_json_string(self, **kw: Any) -> str:
return json.dumps(self.to_json(), **kw)


@dataclass
class Ident:
"""Original type: ident"""
Expand Down Expand Up @@ -3428,7 +3474,7 @@ def to_json_string(self, **kw: Any) -> str:
class Int:
"""Original type: literal = [ ... | Int of ... | ... ]"""

value: IntNullableWrap
value: XB40703a

@property
def kind(self) -> str:
Expand Down Expand Up @@ -3640,7 +3686,7 @@ def from_json(cls, x: Any) -> 'Literal':
if cons == 'Bool':
return cls(Bool(BoolWrap.from_json(x[1])))
if cons == 'Int':
return cls(Int(IntNullableWrap.from_json(x[1])))
return cls(Int(XB40703a.from_json(x[1])))
if cons == 'Float':
return cls(Float(FloatNullableWrap.from_json(x[1])))
if cons == 'Char':
Expand Down Expand Up @@ -5761,7 +5807,7 @@ def to_json_string(self, **kw: Any) -> str:
class ImportFrom:
"""Original type: directive = [ ... | ImportFrom of ... | ... ]"""

value: Tuple[Tok, ModuleName, List[Tuple[Ident, Optional[Alias]]]]
value: Tuple[Tok, ModuleName, List[Tuple[Alias, Optional[Alias]]]]

@property
def kind(self) -> str:
Expand Down Expand Up @@ -5899,7 +5945,7 @@ def from_json(cls, x: Any) -> 'Directive':
if isinstance(x, List) and len(x) == 2:
cons = x[0]
if cons == 'ImportFrom':
return cls(ImportFrom((lambda x: (Tok.from_json(x[0]), ModuleName.from_json(x[1]), _atd_read_list((lambda x: (Ident.from_json(x[0]), _atd_read_nullable(Alias.from_json)(x[1])) if isinstance(x, list) and len(x) == 2 else _atd_bad_json('array of length 2', x)))(x[2])) if isinstance(x, list) and len(x) == 3 else _atd_bad_json('array of length 3', x))(x[1])))
return cls(ImportFrom((lambda x: (Tok.from_json(x[0]), ModuleName.from_json(x[1]), _atd_read_list((lambda x: (Alias.from_json(x[0]), _atd_read_nullable(Alias.from_json)(x[1])) if isinstance(x, list) and len(x) == 2 else _atd_bad_json('array of length 2', x)))(x[2])) if isinstance(x, list) and len(x) == 3 else _atd_bad_json('array of length 3', x))(x[1])))
if cons == 'ImportAs':
return cls(ImportAs((lambda x: (Tok.from_json(x[0]), ModuleName.from_json(x[1]), _atd_read_nullable(Alias.from_json)(x[2])) if isinstance(x, list) and len(x) == 3 else _atd_bad_json('array of length 3', x))(x[1])))
if cons == 'ImportAll':
Expand Down Expand Up @@ -8117,18 +8163,18 @@ def to_json_string(self, **kw: Any) -> str:


@dataclass
class PatUnderscore:
"""Original type: pattern = [ ... | PatUnderscore of ... | ... ]"""
class PatWildcard:
"""Original type: pattern = [ ... | PatWildcard of ... | ... ]"""

value: Tok

@property
def kind(self) -> str:
"""Name of the class representing this variant."""
return 'PatUnderscore'
return 'PatWildcard'

def to_json(self) -> Any:
return ['PatUnderscore', (lambda x: x.to_json())(self.value)]
return ['PatWildcard', (lambda x: x.to_json())(self.value)]

def to_json_string(self, **kw: Any) -> str:
return json.dumps(self.to_json(), **kw)
Expand Down Expand Up @@ -8246,7 +8292,7 @@ def to_json_string(self, **kw: Any) -> str:
class Pattern:
"""Original type: pattern = [ ... ]"""

value: Union[PatLiteral, PatConstructor, PatRecord, PatId, PatTuple, PatList, PatKeyVal, PatUnderscore, PatDisj, PatTyped, PatWhen, PatAs, PatType, OtherPat]
value: Union[PatLiteral, PatConstructor, PatRecord, PatId, PatTuple, PatList, PatKeyVal, PatWildcard, PatDisj, PatTyped, PatWhen, PatAs, PatType, OtherPat]

@property
def kind(self) -> str:
Expand All @@ -8271,8 +8317,8 @@ def from_json(cls, x: Any) -> 'Pattern':
return cls(PatList(PatternListBracket.from_json(x[1])))
if cons == 'PatKeyVal':
return cls(PatKeyVal((lambda x: (Pattern.from_json(x[0]), Pattern.from_json(x[1])) if isinstance(x, list) and len(x) == 2 else _atd_bad_json('array of length 2', x))(x[1])))
if cons == 'PatUnderscore':
return cls(PatUnderscore(Tok.from_json(x[1])))
if cons == 'PatWildcard':
return cls(PatWildcard(Tok.from_json(x[1])))
if cons == 'PatDisj':
return cls(PatDisj((lambda x: (Pattern.from_json(x[0]), Pattern.from_json(x[1])) if isinstance(x, list) and len(x) == 2 else _atd_bad_json('array of length 2', x))(x[1])))
if cons == 'PatTyped':
Expand Down
Loading

0 comments on commit 1d13c0e

Please sign in to comment.