Skip to content

Commit

Permalink
feat: show tips regarding new PEPs
Browse files Browse the repository at this point in the history
  • Loading branch information
Gowee committed Jun 22, 2023
1 parent f827e05 commit fc24201
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
16 changes: 12 additions & 4 deletions src/target/python_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ fn write_output(
options: &PythonClass,
header: &mut dyn Write,
body: &mut dyn Write,
_additional: &mut dyn Write,
additional: &mut dyn Write,
) -> fmt::Result {
let wrapper = with_context((), Context(schema, options)); // helper

Expand Down Expand Up @@ -171,20 +171,28 @@ fn write_output(
writeln!(header, "{}", import)?;
}
if !imports_from_typing.is_empty() {
if imports_from_typing.contains("Union") {
writeln!(additional, "# 💡 Starting from Python 3.10 (PEP 604), `Union[A, B]` can be simplified as `A | B`\n")?;
}
let typing_mod = if ["NotRequired", "Missing"]
.iter()
.any(|&t| imports_from_typing.contains(t))
{
// PEP 655 for now
writeln!(
additional,
r#"# 💡 `NotRequired` or `Missing` are introduced since Python 3.11 (PEP 655).
# `typing_extensions` is imported above for backwards compatibility.
# For Python < 3.11, pip install typing_extensions. O.W., just change it to `typing`\n"#
)?;
"typing_extensions"
} else {
"typing"
};

write!(header, "from {} import ", typing_mod)?;
Itertools::intersperse(imports_from_typing.into_iter(), ", ")
.try_for_each(|e| write!(header, "{}", e))?;
if typing_mod == "typing_extensions" {
write!(header, " # For Python < 3.11, pip install typing_extensions; For Python >= 3.11, just change it to `typing`")?;
}
writeln!(header)?;
}
if importing_datetime {
Expand Down
23 changes: 21 additions & 2 deletions src/target/python_inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ fn write_output(
options: &PythonTypedDict,
header: &mut dyn Write,
body: &mut dyn Write,
_additional: &mut dyn Write,
additional: &mut dyn Write,
) -> fmt::Result {
let mut imports_from_typing = HashSet::new();
let mut importing_base_class_or_class_decorators = false;
Expand Down Expand Up @@ -147,7 +147,26 @@ fn write_output(
}

if importing_base_class_or_class_decorators || !imports_from_typing.is_empty() {
write!(header, "from typing import ")?;
if imports_from_typing.contains("Union") {
writeln!(additional, "# 💡 Starting from Python 3.10 (PEP 604), `Union[A, B]` can be simplified as `A | B`\n")?;
}
let typing_mod = if ["NotRequired", "Missing"]
.iter()
.any(|&t| imports_from_typing.contains(t))
{
// PEP 655 for now
writeln!(
additional,
r#"# 💡 `NotRequired` or `Missing` are introduced since Python 3.11 (PEP 655).
# `typing_extensions` is imported above for backwards compatibility.
# For Python < 3.11, pip install typing_extensions. O.W., just change it to `typing`\n"#
)?;
"typing_extensions"
} else {
"typing"
};

write!(header, "from {} import ", typing_mod)?;
if importing_base_class_or_class_decorators {
write!(header, "TypedDict")?;
if !imports_from_typing.is_empty() {
Expand Down

0 comments on commit fc24201

Please sign in to comment.