Skip to content

Commit

Permalink
Merge pull request #267 from ahrefs/mj-dedup-decorator
Browse files Browse the repository at this point in the history
atdpy fix: Avoid applying the `@dataclass` decorator twice
  • Loading branch information
mjambon authored Mar 25, 2022
2 parents de81fc9 + 59e8322 commit 9fe8cf1
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 5 deletions.
7 changes: 7 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
Next
-----------------

* atdpy: don't apply the `@dataclass` decorator twice if explicitly
added by the user via an ATD annotation such as
`<python decorator="dataclass(frozen=True)">` (#267)

2.4.0 (2022-03-24)
-----------------

Expand Down
1 change: 1 addition & 0 deletions atdpy.opam
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ depends: [
"atd" {>= "2.3.0"}
"atdgen" {>= "2.3.0"}
"cmdliner" {>= "1.1.0"}
"re"
"alcotest" {with-test}
"conf-python-3" {with-test}
"odoc" {with-doc}
Expand Down
18 changes: 14 additions & 4 deletions atdpy/src/lib/Codegen.ml
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,6 @@ let record env ~class_decorators loc name (fields : field list) an =
in
[
Inline class_decorators;
Line "@dataclass";
Line (sprintf "class %s:" py_class_name);
Block (spaced [
Line (sprintf {|"""Original type: %s = { ... }"""|} name);
Expand Down Expand Up @@ -880,7 +879,6 @@ let alias_wrapper env ~class_decorators name type_expr =
let value_type = type_name_of_expr env type_expr in
[
Inline class_decorators;
Line "@dataclass";
Line (sprintf "class %s:" py_class_name);
Block [
Line (sprintf {|"""Original type: %s"""|} name);
Expand Down Expand Up @@ -1058,7 +1056,6 @@ let sum_container env ~class_decorators loc name cases =
in
[
Inline class_decorators;
Line "@dataclass";
Line (sprintf "class %s:" py_class_name);
Block [
Line (sprintf {|"""Original type: %s = [ ... ]"""|} name);
Expand Down Expand Up @@ -1122,11 +1119,24 @@ let sum env ~class_decorators loc name cases =
]
|> double_spaced

let uses_dataclass_decorator =
let rex = Re.Pcre.regexp {|\A[ \t\r\n]*dataclass(\(|[ \t\r\n]|\z)|} in
fun s -> Re.Pcre.pmatch ~rex s

let get_class_decorators an =
let decorators = Python_annot.get_python_decorators an in
(* Avoid duplicate use of the @dataclass decorator, which doesn't work
if some options like frozen=True are used. *)
if List.exists uses_dataclass_decorator decorators then
decorators
else
decorators @ ["dataclass"]

let type_def env ((loc, (name, param, an), e) : A.type_def) : B.t =
if param <> [] then
not_implemented loc "parametrized type";
let class_decorators =
Python_annot.get_python_decorators an
get_class_decorators an
|> List.map (fun s -> Line ("@" ^ s))
in
let rec unwrap e =
Expand Down
1 change: 1 addition & 0 deletions atdpy/src/lib/dune
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(library
(name atdpy)
(libraries
re
atd
atdgen_emit
)
Expand Down
1 change: 0 additions & 1 deletion atdpy/test/python-expected/everything.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,6 @@ def to_json_string(self, **kw: Any) -> str:
@deco.deco1
@deco.deco2(42)
@dataclass(order=True)
@dataclass
class RequireField:
"""Original type: require_field = { ... }"""

Expand Down
1 change: 1 addition & 0 deletions dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ automatically handled")
(atd (>= 2.3.0))
(atdgen (>= 2.3.0))
(cmdliner (>= 1.1.0))
re
(alcotest :with-test)
(conf-python-3 :with-test)
(odoc :with-doc)))
Expand Down

0 comments on commit 9fe8cf1

Please sign in to comment.