Skip to content

Commit

Permalink
[twimage][tests] Fix fbcode//tupperware/cm/antlir:cm.shape-python-typ…
Browse files Browse the repository at this point in the history
…e-checking

Test Plan:
```
buck2 test fbcode//tupperware/cm/antlir:cm.shape-python-type-checking \
    -- --run-disabled
```

Reviewed By: naveedgol

Differential Revision: D64215740

fbshipit-source-id: 2cfbd685c8ccfa43ac98c2dd69e97dd3703f813c
  • Loading branch information
epilatow authored and facebook-github-bot committed Oct 11, 2024
1 parent 09a78ef commit ec02f56
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
20 changes: 20 additions & 0 deletions antlir/bzl/shape2/ir2code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ impl RegisterTemplates<Pydantic> for Module {
templates.read_template("module.pydantic.handlebars")?,
)
.context("Trying to register preamble template")?;
hb.register_helper("lint-directive", Box::new(py_lint_directive));
hb.register_helper("type-hint", Box::new(py_type_hint));
hb.register_helper("literal", Box::new(py_literal));
Ok(hb)
Expand Down Expand Up @@ -391,6 +392,25 @@ impl TypeExt for Type {
}
}

trait FieldExt {
fn py_lint_directive(&self) -> &str;
}

impl FieldExt for Field {
fn py_lint_directive(&self) -> &str {
if let Type::List { .. } = *self.ty {
// In py_type_hint() we lie and convert Lists to Tuples to
// discourage mutation, so here we pay penance by suppressing
// typing errors
" # pyre-ignore: Incompatible attribute type [8]:"
} else {
""
}
}
}

handlebars_helper!(py_lint_directive: |field: Field| field.py_lint_directive().to_string());

handlebars_helper!(has_default_value: |field: Field| Value::Bool(field.default_value.is_some()));
handlebars_helper!(py_type_hint: |ty: Type| ty.py_type_hint().to_string());

Expand Down
4 changes: 2 additions & 2 deletions antlir/bzl/shape2/templates/struct.pydantic.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ class {{name}}(Shape):

{{#each fields}}
{{#if required}}
{{@key}}: {{type-hint type}} {{#if (has-default-value this)}}= {{literal default_value type}}{{/if}}
{{@key}}: {{type-hint type}} {{#if (has-default-value this)}}= {{literal default_value type}}{{lint-directive this}}{{/if}}
{{else}}
{{@key}}: typing.Optional[{{type-hint type}}] = {{#if (has-default-value this)}}{{literal default_value type}}{{else}}None{{/if}}
{{@key}}: typing.Optional[{{type-hint type}}] = {{#if (has-default-value this)}}{{literal default_value type}}{{else}}None{{lint-directive this}}{{/if}}
{{/if}}
{{/each}}

0 comments on commit ec02f56

Please sign in to comment.