-
Notifications
You must be signed in to change notification settings - Fork 200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
changes to some coercions #208
Merged
Merged
Changes from 3 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
cf82484
stop coercing `set / frozenset` to `list / tuple`
PrettyWood db47669
add `dict_key` and `dict_value`
PrettyWood 20e37a7
add iterator support
PrettyWood bbaab55
more tests
samuelcolvin 04c4c13
use PyList directly for lax_list
PrettyWood e185685
catch errors in generator evaluation
samuelcolvin f806d8c
fix mypy and more tests
samuelcolvin cdef295
remove unused dict_items code
samuelcolvin 7c9e7d6
Merge branch 'main' into coercion-tweaks
samuelcolvin 49df207
make format
PrettyWood File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// TODO: remove this file once a new pyo3 version is released | ||
// with https://github.com/PyO3/pyo3/pull/2358 | ||
|
||
use pyo3::{ffi, pyobject_native_type_core, PyAny}; | ||
|
||
/// Represents a Python `dict_keys`. | ||
#[cfg(not(PyPy))] | ||
#[repr(transparent)] | ||
pub struct PyDictKeys(PyAny); | ||
|
||
#[cfg(not(PyPy))] | ||
pyobject_native_type_core!( | ||
PyDictKeys, | ||
ffi::PyDictKeys_Type, | ||
#checkfunction=ffi::PyDictKeys_Check | ||
); | ||
|
||
/// Represents a Python `dict_values`. | ||
#[cfg(not(PyPy))] | ||
#[repr(transparent)] | ||
pub struct PyDictValues(PyAny); | ||
|
||
#[cfg(not(PyPy))] | ||
pyobject_native_type_core!( | ||
PyDictValues, | ||
ffi::PyDictValues_Type, | ||
#checkfunction=ffi::PyDictValues_Check | ||
); | ||
|
||
/// Represents a Python `dict_items`. | ||
#[cfg(not(PyPy))] | ||
#[repr(transparent)] | ||
pub struct PyDictItems(PyAny); | ||
|
||
#[cfg(not(PyPy))] | ||
pyobject_native_type_core!( | ||
PyDictItems, | ||
ffi::PyDictItems_Type, | ||
#checkfunction=ffi::PyDictItems_Check | ||
); | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,13 +4,15 @@ use std::str::from_utf8; | |
use pyo3::exceptions::PyAttributeError; | ||
use pyo3::prelude::*; | ||
use pyo3::types::{ | ||
PyBool, PyByteArray, PyBytes, PyDate, PyDateTime, PyDelta, PyDict, PyFrozenSet, PyInt, PyList, PyMapping, | ||
PySequence, PySet, PyString, PyTime, PyTuple, PyType, | ||
PyBool, PyByteArray, PyBytes, PyDate, PyDateTime, PyDelta, PyDict, PyFrozenSet, PyInt, PyIterator, PyList, | ||
PyMapping, PySequence, PySet, PyString, PyTime, PyTuple, PyType, | ||
}; | ||
use pyo3::{intern, AsPyPointer}; | ||
|
||
use crate::errors::{py_err_string, ErrorKind, InputValue, LocItem, ValError, ValResult}; | ||
|
||
#[cfg(not(PyPy))] | ||
use super::_pyo3_dict::{PyDictKeys, PyDictValues}; | ||
use super::datetime::{ | ||
bytes_as_date, bytes_as_datetime, bytes_as_time, bytes_as_timedelta, date_as_datetime, float_as_datetime, | ||
float_as_duration, float_as_time, int_as_datetime, int_as_duration, int_as_time, EitherDate, EitherDateTime, | ||
|
@@ -271,15 +273,35 @@ impl<'a> Input<'a> for PyAny { | |
} | ||
} | ||
|
||
#[cfg(not(PyPy))] | ||
fn lax_list(&'a self) -> ValResult<GenericListLike<'a>> { | ||
if let Ok(list) = self.cast_as::<PyList>() { | ||
Ok(list.into()) | ||
} else if let Ok(tuple) = self.cast_as::<PyTuple>() { | ||
Ok(tuple.into()) | ||
} else if let Ok(set) = self.cast_as::<PySet>() { | ||
Ok(set.into()) | ||
} else if let Ok(frozen_set) = self.cast_as::<PyFrozenSet>() { | ||
Ok(frozen_set.into()) | ||
} else if let Ok(iterator) = self.cast_as::<PyIterator>() { | ||
let tuple = PyTuple::new(self.py(), iterator.iter()?.flatten().collect::<Vec<_>>()); | ||
samuelcolvin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Ok(tuple.into()) | ||
} else if let Ok(dict_keys) = self.cast_as::<PyDictKeys>() { | ||
let tuple = PyTuple::new(self.py(), dict_keys.iter()?.flatten().collect::<Vec<_>>()); | ||
Ok(tuple.into()) | ||
} else if let Ok(dict_values) = self.cast_as::<PyDictValues>() { | ||
let tuple = PyTuple::new(self.py(), dict_values.iter()?.flatten().collect::<Vec<_>>()); | ||
Ok(tuple.into()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like we have this lump of code in multiple cases, could you move it into a macro (or parameterised function?) and reuse? |
||
} else { | ||
Err(ValError::new(ErrorKind::ListType, self)) | ||
} | ||
} | ||
|
||
#[cfg(PyPy)] | ||
fn lax_list(&'a self) -> ValResult<GenericListLike<'a>> { | ||
if let Ok(list) = self.cast_as::<PyList>() { | ||
Ok(list.into()) | ||
} else if let Ok(tuple) = self.cast_as::<PyTuple>() { | ||
Ok(tuple.into()) | ||
} else if let Ok(iterator) = self.cast_as::<PyIterator>() { | ||
let tuple = PyTuple::new(self.py(), iterator.iter()?.flatten().collect::<Vec<_>>()); | ||
Ok(tuple.into()) | ||
} else { | ||
Err(ValError::new(ErrorKind::ListType, self)) | ||
} | ||
|
@@ -293,15 +315,35 @@ impl<'a> Input<'a> for PyAny { | |
} | ||
} | ||
|
||
#[cfg(not(PyPy))] | ||
fn lax_tuple(&'a self) -> ValResult<GenericListLike<'a>> { | ||
if let Ok(tuple) = self.cast_as::<PyTuple>() { | ||
Ok(tuple.into()) | ||
} else if let Ok(list) = self.cast_as::<PyList>() { | ||
Ok(list.into()) | ||
} else if let Ok(set) = self.cast_as::<PySet>() { | ||
Ok(set.into()) | ||
} else if let Ok(frozen_set) = self.cast_as::<PyFrozenSet>() { | ||
Ok(frozen_set.into()) | ||
} else if let Ok(iterator) = self.cast_as::<PyIterator>() { | ||
let tuple = PyTuple::new(self.py(), iterator.iter()?.flatten().collect::<Vec<_>>()); | ||
Ok(tuple.into()) | ||
} else if let Ok(dict_keys) = self.cast_as::<PyDictKeys>() { | ||
let tuple = PyTuple::new(self.py(), dict_keys.iter()?.flatten().collect::<Vec<_>>()); | ||
Ok(tuple.into()) | ||
} else if let Ok(dict_values) = self.cast_as::<PyDictValues>() { | ||
let tuple = PyTuple::new(self.py(), dict_values.iter()?.flatten().collect::<Vec<_>>()); | ||
Ok(tuple.into()) | ||
} else { | ||
Err(ValError::new(ErrorKind::TupleType, self)) | ||
} | ||
} | ||
|
||
#[cfg(PyPy)] | ||
fn lax_tuple(&'a self) -> ValResult<GenericListLike<'a>> { | ||
if let Ok(tuple) = self.cast_as::<PyTuple>() { | ||
Ok(tuple.into()) | ||
} else if let Ok(list) = self.cast_as::<PyList>() { | ||
Ok(list.into()) | ||
} else if let Ok(iterator) = self.cast_as::<PyIterator>() { | ||
let tuple = PyTuple::new(self.py(), iterator.iter()?.flatten().collect::<Vec<_>>()); | ||
Ok(tuple.into()) | ||
} else { | ||
Err(ValError::new(ErrorKind::TupleType, self)) | ||
} | ||
|
@@ -315,6 +357,7 @@ impl<'a> Input<'a> for PyAny { | |
} | ||
} | ||
|
||
#[cfg(not(PyPy))] | ||
fn lax_set(&'a self) -> ValResult<GenericListLike<'a>> { | ||
if let Ok(set) = self.cast_as::<PySet>() { | ||
Ok(set.into()) | ||
|
@@ -324,6 +367,33 @@ impl<'a> Input<'a> for PyAny { | |
Ok(tuple.into()) | ||
} else if let Ok(frozen_set) = self.cast_as::<PyFrozenSet>() { | ||
Ok(frozen_set.into()) | ||
} else if let Ok(iterator) = self.cast_as::<PyIterator>() { | ||
let tuple = PyTuple::new(self.py(), iterator.iter()?.flatten().collect::<Vec<_>>()); | ||
Ok(tuple.into()) | ||
} else if let Ok(dict_keys) = self.cast_as::<PyDictKeys>() { | ||
let tuple = PyTuple::new(self.py(), dict_keys.iter()?.flatten().collect::<Vec<_>>()); | ||
Ok(tuple.into()) | ||
} else if let Ok(dict_values) = self.cast_as::<PyDictValues>() { | ||
let tuple = PyTuple::new(self.py(), dict_values.iter()?.flatten().collect::<Vec<_>>()); | ||
Ok(tuple.into()) | ||
} else { | ||
Err(ValError::new(ErrorKind::SetType, self)) | ||
} | ||
} | ||
|
||
#[cfg(PyPy)] | ||
fn lax_set(&'a self) -> ValResult<GenericListLike<'a>> { | ||
if let Ok(set) = self.cast_as::<PySet>() { | ||
Ok(set.into()) | ||
} else if let Ok(list) = self.cast_as::<PyList>() { | ||
Ok(list.into()) | ||
} else if let Ok(tuple) = self.cast_as::<PyTuple>() { | ||
Ok(tuple.into()) | ||
} else if let Ok(frozen_set) = self.cast_as::<PyFrozenSet>() { | ||
Ok(frozen_set.into()) | ||
} else if let Ok(iterator) = self.cast_as::<PyIterator>() { | ||
let tuple = PyTuple::new(self.py(), iterator.iter()?.flatten().collect::<Vec<_>>()); | ||
Ok(tuple.into()) | ||
} else { | ||
Err(ValError::new(ErrorKind::SetType, self)) | ||
} | ||
|
@@ -337,6 +407,7 @@ impl<'a> Input<'a> for PyAny { | |
} | ||
} | ||
|
||
#[cfg(not(PyPy))] | ||
fn lax_frozenset(&'a self) -> ValResult<GenericListLike<'a>> { | ||
if let Ok(frozen_set) = self.cast_as::<PyFrozenSet>() { | ||
Ok(frozen_set.into()) | ||
|
@@ -346,6 +417,33 @@ impl<'a> Input<'a> for PyAny { | |
Ok(list.into()) | ||
} else if let Ok(tuple) = self.cast_as::<PyTuple>() { | ||
Ok(tuple.into()) | ||
} else if let Ok(iterator) = self.cast_as::<PyIterator>() { | ||
let tuple = PyTuple::new(self.py(), iterator.iter()?.flatten().collect::<Vec<_>>()); | ||
Ok(tuple.into()) | ||
} else if let Ok(dict_keys) = self.cast_as::<PyDictKeys>() { | ||
let tuple = PyTuple::new(self.py(), dict_keys.iter()?.flatten().collect::<Vec<_>>()); | ||
Ok(tuple.into()) | ||
} else if let Ok(dict_values) = self.cast_as::<PyDictValues>() { | ||
let tuple = PyTuple::new(self.py(), dict_values.iter()?.flatten().collect::<Vec<_>>()); | ||
Ok(tuple.into()) | ||
} else { | ||
Err(ValError::new(ErrorKind::FrozenSetType, self)) | ||
} | ||
} | ||
|
||
#[cfg(PyPy)] | ||
fn lax_frozenset(&'a self) -> ValResult<GenericListLike<'a>> { | ||
if let Ok(frozen_set) = self.cast_as::<PyFrozenSet>() { | ||
Ok(frozen_set.into()) | ||
} else if let Ok(set) = self.cast_as::<PySet>() { | ||
Ok(set.into()) | ||
} else if let Ok(list) = self.cast_as::<PyList>() { | ||
Ok(list.into()) | ||
} else if let Ok(tuple) = self.cast_as::<PyTuple>() { | ||
Ok(tuple.into()) | ||
} else if let Ok(iterator) = self.cast_as::<PyIterator>() { | ||
let tuple = PyTuple::new(self.py(), iterator.iter()?.flatten().collect::<Vec<_>>()); | ||
Ok(tuple.into()) | ||
} else { | ||
Err(ValError::new(ErrorKind::FrozenSetType, self)) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
use pyo3::prelude::*; | ||
|
||
#[cfg(not(PyPy))] | ||
mod _pyo3_dict; | ||
mod datetime; | ||
mod input_abstract; | ||
mod input_json; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we remove this as I think it's not used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes!