Skip to content

Commit 2bfd37b

Browse files
stepanchegfacebook-github-bot
authored andcommitted
Option<Ty> -> Ty
Reviewed By: ndmitchell, JakobDegen Differential Revision: D47173482 fbshipit-source-id: a2e4a807a88139545c8c696ebb9b6cf86ece4ccd
1 parent 0f4d461 commit 2bfd37b

File tree

8 files changed

+68
-74
lines changed

8 files changed

+68
-74
lines changed

starlark/src/docs/markdown.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ const MAX_LENGTH_BEFORE_MULTILINE: usize = 80;
275275
/// produce a function prototype.
276276
enum TypeRenderer<'a> {
277277
/// A general "type".
278-
Type(&'a Option<Ty>),
278+
Type(&'a Ty),
279279
/// A function, with some extra formatting options.
280280
Function {
281281
/// The function name in the prototype as well.
@@ -286,18 +286,14 @@ enum TypeRenderer<'a> {
286286

287287
impl<'a> RenderMarkdown for TypeRenderer<'a> {
288288
fn render_markdown_opt(&self, flavor: MarkdownFlavor) -> Option<String> {
289-
fn raw_type(t: &Option<Ty>) -> String {
290-
match t {
291-
Some(t) => t.to_string(),
292-
_ => "\"\"".to_owned(),
293-
}
289+
fn raw_type(t: &Ty) -> String {
290+
t.to_string()
294291
}
295292

296-
fn raw_type_prefix(prefix: &str, t: &Option<Ty>) -> String {
297-
if t.is_some() {
298-
format!("{prefix}{}", raw_type(t))
299-
} else {
300-
String::new()
293+
fn raw_type_prefix(prefix: &str, t: &Ty) -> String {
294+
match t {
295+
Ty::Any => String::new(),
296+
t => format!("{prefix}{}", raw_type(t)),
301297
}
302298
}
303299

starlark/src/docs/mod.rs

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ impl DocModule {
365365
}
366366

367367
/// Documents a single function.
368-
#[derive(Debug, Clone, PartialEq, Serialize, Default, Allocative)]
368+
#[derive(Debug, Clone, PartialEq, Default, Serialize, Allocative)]
369369
pub struct DocFunction {
370370
/// Documentation for the function. If parsed, this should generally be the first statement
371371
/// of a function's body if that statement is a string literal. Any sections like "Args:",
@@ -445,10 +445,8 @@ impl DocFunction {
445445
ds
446446
})
447447
.unwrap_or_default();
448-
let ret = self
449-
.ret
450-
.typ
451-
.as_ref()
448+
let ret = Some(&self.ret.typ)
449+
.filter(|t| t != &&Ty::Any)
452450
.map(|t| format!(" -> {}", t))
453451
.unwrap_or_default();
454452

@@ -468,7 +466,7 @@ impl DocFunction {
468466
pub fn from_docstring(
469467
kind: DocStringKind,
470468
mut params: Vec<DocParam>,
471-
return_type: Option<Ty>,
469+
return_type: Ty,
472470
raw_docstring: Option<&str>,
473471
dot_type: Option<String>,
474472
) -> Self {
@@ -581,7 +579,7 @@ pub enum DocParam {
581579
name: String,
582580
docs: Option<DocString>,
583581
#[serde(rename = "type")]
584-
typ: Option<Ty>,
582+
typ: Ty,
585583
/// If present, this parameter has a default value. This is the `repr()` of that value.
586584
default_value: Option<String>,
587585
},
@@ -594,14 +592,14 @@ pub enum DocParam {
594592
name: String,
595593
docs: Option<DocString>,
596594
#[serde(rename = "type")]
597-
typ: Option<Ty>,
595+
typ: Ty,
598596
},
599597
/// Represents the "**kwargs" style of argument.
600598
Kwargs {
601599
name: String,
602600
docs: Option<DocString>,
603601
#[serde(rename = "type")]
604-
typ: Option<Ty>,
602+
typ: Ty,
605603
},
606604
}
607605

@@ -626,31 +624,38 @@ impl DocParam {
626624
typ,
627625
default_value,
628626
..
629-
} => match (typ.as_ref(), default_value.as_ref()) {
630-
(Some(t), Some(default)) => format!("{}: {} = {}", name, t, default),
631-
(Some(t), None) => format!("{}: {}", name, t),
632-
(None, Some(default)) => format!("{} = {}", name, default),
633-
(None, None) => name.clone(),
627+
} => match (typ, default_value.as_ref()) {
628+
(Ty::Any, Some(default)) => format!("{} = {}", name, default),
629+
(Ty::Any, None) => name.clone(),
630+
(t, Some(default)) => format!("{}: {} = {}", name, t, default),
631+
(t, None) => format!("{}: {}", name, t),
634632
},
635633
DocParam::NoArgs => "*".to_owned(),
636634
DocParam::OnlyPosBefore => "/".to_owned(),
637-
DocParam::Args { name, typ, .. } | DocParam::Kwargs { name, typ, .. } => {
638-
match typ.as_ref() {
639-
Some(typ) => format!("{}: {}", name, typ),
640-
None => name.clone(),
641-
}
642-
}
635+
DocParam::Args { name, typ, .. } | DocParam::Kwargs { name, typ, .. } => match typ {
636+
Ty::Any => name.clone(),
637+
typ => format!("{}: {}", name, typ),
638+
},
643639
}
644640
}
645641
}
646642

647643
/// Details about the return value of a function.
648-
#[derive(Debug, Clone, PartialEq, Serialize, Default, Allocative)]
644+
#[derive(Debug, Clone, PartialEq, Serialize, Allocative)]
649645
pub struct DocReturn {
650646
/// Extra semantic details around the returned value's meaning.
651647
pub docs: Option<DocString>,
652648
#[serde(rename = "type")]
653-
pub typ: Option<Ty>,
649+
pub typ: Ty,
650+
}
651+
652+
impl Default for DocReturn {
653+
fn default() -> Self {
654+
DocReturn {
655+
docs: None,
656+
typ: Ty::Any,
657+
}
658+
}
654659
}
655660

656661
impl DocReturn {
@@ -660,17 +665,17 @@ impl DocReturn {
660665
}
661666

662667
/// A single property of an object. These are explicitly not functions (see [`DocMember`]).
663-
#[derive(Debug, Clone, PartialEq, Serialize, Default, Allocative)]
668+
#[derive(Debug, Clone, PartialEq, Serialize, Allocative)]
664669
pub struct DocProperty {
665670
pub docs: Option<DocString>,
666671
#[serde(rename = "type")]
667-
pub typ: Option<Ty>,
672+
pub typ: Ty,
668673
}
669674

670675
impl DocProperty {
671676
fn render_as_code(&self, name: &str) -> String {
672677
match (
673-
self.typ.as_ref(),
678+
&self.typ,
674679
self.docs.as_ref().map(DocString::render_as_quoted_code),
675680
) {
676681
// TODO(nmj): The starlark syntax needs to be updated to support type
@@ -680,13 +685,12 @@ impl DocProperty {
680685
// format!("{}\n_{}: {} = None", ds, name, t.raw_type)
681686
// }
682687
// (Some(t), None) => format!(r#"_{}: {} = None"#, name, t.raw_type),
683-
(Some(t), Some(ds)) => {
688+
(Ty::Any, Some(ds)) => format!("{}\n_{} = None", ds, name),
689+
(Ty::Any, None) => format!("_{} = None", name),
690+
(t, Some(ds)) => {
684691
format!("{}\n# type: {}\n_{} = None", ds, t, name)
685692
}
686-
(Some(t), None) => format!("# type: {}\n_{} = None", t, name),
687-
688-
(None, Some(ds)) => format!("{}\n_{} = None", ds, name),
689-
(None, None) => format!("_{} = None", name),
693+
(t, None) => format!("# type: {}\n_{} = None", t, name),
690694
}
691695
}
692696
}
@@ -708,7 +712,7 @@ impl DocMember {
708712
Some(DocItem::Property(x)) => DocMember::Property(x),
709713
_ => DocMember::Property(DocProperty {
710714
docs: None,
711-
typ: Some(value.get_type_starlark_repr()),
715+
typ: value.get_type_starlark_repr(),
712716
}),
713717
}
714718
}
@@ -1134,7 +1138,7 @@ mod tests {
11341138
DocParam::Arg {
11351139
name: name.to_owned(),
11361140
docs: None,
1137-
typ: None,
1141+
typ: Ty::Any,
11381142
default_value: None,
11391143
}
11401144
}
@@ -1158,20 +1162,20 @@ mod tests {
11581162
"#;
11591163

11601164
let kind = DocStringKind::Starlark;
1161-
let return_type = Some(Ty::int());
1165+
let return_type = Ty::int();
11621166
let expected = DocFunction {
11631167
docs: DocString::from_docstring(kind, "This is an example docstring\n\nDetails here"),
11641168
params: vec![
11651169
DocParam::Arg {
11661170
name: "**kwargs".to_owned(),
11671171
docs: DocString::from_docstring(kind, "Docs for kwargs"),
1168-
typ: None,
1172+
typ: Ty::Any,
11691173
default_value: None,
11701174
},
11711175
DocParam::Arg {
11721176
name: "*args".to_owned(),
11731177
docs: DocString::from_docstring(kind, "Docs for args"),
1174-
typ: None,
1178+
typ: Ty::Any,
11751179
default_value: None,
11761180
},
11771181
DocParam::Arg {
@@ -1184,13 +1188,13 @@ mod tests {
11841188
"over three lines"
11851189
),
11861190
),
1187-
typ: None,
1191+
typ: Ty::Any,
11881192
default_value: None,
11891193
},
11901194
DocParam::Arg {
11911195
name: "arg_foo".to_owned(),
11921196
docs: DocString::from_docstring(kind, "The argument named foo"),
1193-
typ: None,
1197+
typ: Ty::Any,
11941198
default_value: None,
11951199
},
11961200
],
@@ -1234,7 +1238,7 @@ mod tests {
12341238
"#;
12351239

12361240
let kind = DocStringKind::Rust;
1237-
let return_type = Some(Ty::int());
1241+
let return_type = Ty::int();
12381242
let expected = DocFunction {
12391243
docs: DocString::from_docstring(kind, "This is an example docstring\n\nDetails here"),
12401244
params: vec![
@@ -1248,13 +1252,13 @@ mod tests {
12481252
"over three lines"
12491253
),
12501254
),
1251-
typ: None,
1255+
typ: Ty::Any,
12521256
default_value: None,
12531257
},
12541258
DocParam::Arg {
12551259
name: "arg_foo".to_owned(),
12561260
docs: DocString::from_docstring(kind, "The argument named foo"),
1257-
typ: None,
1261+
typ: Ty::Any,
12581262
default_value: None,
12591263
},
12601264
],

starlark/src/eval/compiler/def.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ impl<'v, T1: ValueLike<'v>> DefGen<T1> {
550550
.map(|(idx, _, ty)| (idx.0 as usize, ty.as_ty()))
551551
.collect();
552552

553-
let return_type = self.return_type.map(|r| r.as_ty());
553+
let return_type = self.return_type.map_or(Ty::Any, |r| r.as_ty());
554554

555555
let function_docs = DocFunction::from_docstring(
556556
DocStringKind::Starlark,

starlark/src/eval/runtime/params.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ impl<'v, V: ValueLike<'v>> ParametersSpec<V> {
664664
.iter_params()
665665
.enumerate()
666666
.map(|(i, (name, kind))| {
667-
let typ = parameter_types.remove(&i);
667+
let typ = parameter_types.remove(&i).unwrap_or(Ty::Any);
668668
let docs = parameter_docs.remove(name).flatten();
669669
if pos_only == i
670670
&& !matches!(kind, ParameterKind::Args | ParameterKind::KWargs)
@@ -859,18 +859,18 @@ mod tests {
859859
DocParam::Args {
860860
name: "*args".to_owned(),
861861
docs: None,
862-
typ: None,
862+
typ: Ty::Any,
863863
},
864864
DocParam::Arg {
865865
name: "a".to_owned(),
866866
docs: None,
867-
typ: Some(Ty::int()),
867+
typ: Ty::int(),
868868
default_value: Some("_".to_owned()),
869869
},
870870
DocParam::Arg {
871871
name: "b".to_owned(),
872872
docs: DocString::from_docstring(DocStringKind::Rust, "param b docs"),
873-
typ: None,
873+
typ: Ty::Any,
874874
default_value: Some("_".to_owned()),
875875
},
876876
];

starlark/src/tests/docs/golden/module.golden.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ And some assertions:
7979
## notypes
8080

8181
```python
82-
def notypes(a: "") -> ""
82+
def notypes(a)
8383
```
8484

8585
---

starlark/src/typing/ty.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -809,13 +809,13 @@ impl Ty {
809809

810810
pub(crate) fn from_docs_member(member: &DocMember) -> Self {
811811
match member {
812-
DocMember::Property(x) => Self::from_docs_type(&x.typ),
812+
DocMember::Property(x) => x.typ.clone(),
813813
DocMember::Function(x) => Self::from_docs_function(x),
814814
}
815815
}
816816

817817
pub(crate) fn from_docs_property(property: &DocProperty) -> Self {
818-
Self::from_docs_type(&property.typ)
818+
property.typ.clone()
819819
}
820820

821821
pub(crate) fn from_docs_function(function: &DocFunction) -> Self {
@@ -830,9 +830,9 @@ impl Ty {
830830
..
831831
} => {
832832
let mut r = if seen_no_args {
833-
Param::name_only(name, Ty::from_docs_type(typ))
833+
Param::name_only(name, typ.clone())
834834
} else {
835-
Param::pos_or_name(name, Ty::from_docs_type(typ))
835+
Param::pos_or_name(name, typ.clone())
836836
};
837837
if default_value.is_some() {
838838
r = r.optional();
@@ -849,24 +849,17 @@ impl Ty {
849849
DocParam::NoArgs => seen_no_args = true,
850850
DocParam::Args { typ, .. } => {
851851
seen_no_args = true;
852-
params.push(Param::args(Ty::from_docs_type(typ)))
852+
params.push(Param::args(typ.clone()))
853853
}
854-
DocParam::Kwargs { typ, .. } => params.push(Param::kwargs(Ty::from_docs_type(typ))),
854+
DocParam::Kwargs { typ, .. } => params.push(Param::kwargs(typ.clone())),
855855
}
856856
}
857-
let result = Self::from_docs_type(&function.ret.typ);
857+
let result = function.ret.typ.clone();
858858
match &function.dot_type {
859859
None => Ty::function(params, result),
860860
Some(type_attr) => Ty::ctor_function(type_attr, params, result),
861861
}
862862
}
863-
864-
pub(crate) fn from_docs_type(ty: &Option<Ty>) -> Self {
865-
match ty {
866-
None => Ty::Any,
867-
Some(x) => x.clone(),
868-
}
869-
}
870863
}
871864

872865
impl Display for Ty {

starlark/src/values/types/function.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ impl NativeCallableRawDocs {
168168
DocStringKind::Rust,
169169
self.signature
170170
.documentation(self.parameter_types.clone(), HashMap::new()),
171-
Some(self.return_type.clone()),
171+
self.return_type.clone(),
172172
self.rust_docstring,
173173
self.dot_type.map(ToOwned::to_owned),
174174
)
@@ -408,7 +408,7 @@ impl<'v> StarlarkValue<'v> for NativeAttribute {
408408
.docstring
409409
.as_ref()
410410
.and_then(|ds| DocString::from_docstring(DocStringKind::Rust, ds));
411-
let typ = Some(self.typ.clone());
411+
let typ = self.typ.clone();
412412
Some(DocItem::Property(DocProperty { docs: ds, typ }))
413413
}
414414
}

0 commit comments

Comments
 (0)