Skip to content

Commit

Permalink
Merge 45e22f3 into 38a0d44
Browse files Browse the repository at this point in the history
  • Loading branch information
orpuente-MS authored Mar 28, 2024
2 parents 38a0d44 + 45e22f3 commit 9cebb9a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 46 deletions.
15 changes: 1 addition & 14 deletions pip/qsharp/_qsharp.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,25 +78,12 @@ def init(

try:
(_, file_contents) = read_file(qsharp_json)
manifest_descriptor["manifest"] = file_contents
except Exception as e:
raise QSharpError(
f"Error reading {qsharp_json}. qsharp.json should exist at the project root and be a valid JSON file."
) from e

try:
manifest_descriptor["manifest"] = json.loads(file_contents)
except Exception as e:
raise QSharpError(
f"Error parsing {qsharp_json}. qsharp.json should exist at the project root and be a valid JSON file."
) from e

# if no features were passed in as an argument, use the features from the manifest.
# this way we prefer the features from the argument over those from the manifest.
if language_features == [] and manifest_descriptor != None:
language_features = (
manifest_descriptor["manifest"].get("languageFeatures") or []
)

_interpreter = Interpreter(
target_profile,
language_features,
Expand Down
51 changes: 19 additions & 32 deletions pip/src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,19 @@ impl FromPyObject<'_> for PyManifestDescriptor {
let manifest_dir = get_dict_opt_string(dict, "manifest_dir")?.ok_or(
PyException::new_err("missing key `manifest_dir` in manifest descriptor"),
)?;
let manifest = dict
.get_item("manifest")?
.ok_or(PyException::new_err(
"missing key `manifest` in manifest descriptor",
))?
.downcast::<PyDict>()?;

let language_features = get_dict_opt_list_string(manifest, "features")?;
let manifest = get_dict_opt_string(dict, "manifest")?.ok_or(PyException::new_err(
"missing key `manifest` in manifest descriptor",
))?;

let manifest = serde_json::from_str::<Manifest>(&manifest).map_err(|_| {
PyErr::new::<PyException, _>(format!(
"Error parsing {manifest_dir}. Manifest should be a valid JSON file."
))
})?;

Ok(Self(ManifestDescriptor {
manifest: Manifest {
author: get_dict_opt_string(manifest, "author")?,
license: get_dict_opt_string(manifest, "license")?,
language_features,
lints: vec![],
},
manifest,
manifest_dir: manifest_dir.into(),
}))
}
Expand All @@ -113,7 +110,15 @@ impl Interpreter {
TargetProfile::Unrestricted => Profile::Unrestricted,
TargetProfile::Base => Profile::Base,
};
let language_features = language_features.unwrap_or_default();
// If no features were passed in as an argument, use the features from the manifest.
// this way we prefer the features from the argument over those from the manifest.
let language_features: Vec<String> = match (language_features, &manifest_descriptor) {
(Some(language_features), _) if !language_features.is_empty() => language_features,
(_, Some(manifest_descriptor)) => {
manifest_descriptor.0.manifest.language_features.clone()
}
_ => vec![],
};

let sources = if let Some(manifest_descriptor) = manifest_descriptor {
let project = file_system(
Expand Down Expand Up @@ -599,21 +604,3 @@ fn get_dict_opt_string(dict: &PyDict, key: &str) -> PyResult<Option<String>> {
None => None,
})
}
fn get_dict_opt_list_string(dict: &PyDict, key: &str) -> PyResult<Vec<String>> {
let value = dict.get_item(key)?;
let list: &PyList = match value {
Some(item) => item.downcast::<PyList>()?,
None => return Ok(vec![]),
};
match list
.iter()
.map(|item| {
item.downcast::<PyString>()
.map(|s| s.to_string_lossy().into())
})
.collect::<std::result::Result<Vec<String>, _>>()
{
Ok(list) => Ok(list),
Err(e) => Err(e.into()),
}
}

0 comments on commit 9cebb9a

Please sign in to comment.