-
Notifications
You must be signed in to change notification settings - Fork 792
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
Implementing iterators in PyMappingProtocol #611
Comments
Thank you for reporting. |
I guess the problem is that |
Would that explain |
After spending quite some time digging around CPython, it looks like
This means that both #[pyproto]
impl PyMappingProtocol for PyMapping {
fn __getitem__(&self, item: String) -> PyResult<String> {
Ok(String::new())
}
}
#[pyproto]
impl PySequenceProtocol for PyMapping {
fn __contains__(&self, word: String) -> PyResult<bool> {
Ok(self.map.contains_key(&word))
}
}
#[pyproto]
impl PyIterProtocol for PyMapping {
fn __iter__(slf: PyRefMut<Self>) -> PyResult<PyObject> {
let mapping = &*slf;
let gil = Python::acquire_gil();
let py = gil.python();
let iter = IntoPy::into_py(
Py::new(py, PyMappingIter::new(mapping.map.iter().map(|(k, _)| k.to_owned()).collect()))?,
py,
);
Ok(iter)
}
} import mapping
m = mapping.Mapping(["Test", "test", "t"])
"Test" in m # calls PySequenceMapping::__contains__
print([x for x in m]) # calls PyIterProtocol::__iter__ So, it seems like
|
In order to get arbitrary keys, PyMappingProtocol::__getitem__ needs to be implemented. To get O(1) __contains__, PySequenceProtocol::__contains__ needs to be implemented. To get proper Iteration support, PyIterProtocol::__iter__ needs to be implemented. PyO3/pyo3#611 This commit adds the correct implementation of the three traits to PyVocab.
In order to get arbitrary keys, PyMappingProtocol::__getitem__ needs to be implemented. To get O(1) __contains__, PySequenceProtocol::__contains__ needs to be implemented. To get proper Iteration support, PyIterProtocol::__iter__ needs to be implemented. PyO3/pyo3#611 This commit adds the correct implementation of the three traits to PyVocab.
Thanks, your analysis looks spot-on to me! I guess we should remove |
The methods are not expected by CPython and are only explicitly callable. To get iteration support, PyIterProtocol should be implemented and to get support for `x in mapping`, PySequenceProtocol's __contains__ should be implemented. PyO3#611
The methods are not expected by CPython and are only explicitly callable. To get iteration support, PyIterProtocol should be implemented and to get support for `x in mapping`, PySequenceProtocol's __contains__ should be implemented. PyO3#611
The methods are not expected by CPython and are only explicitly callable. To get iteration support, PyIterProtocol should be implemented and to get support for `x in mapping`, PySequenceProtocol's __contains__ should be implemented. PyO3#611
In order to get arbitrary keys, PyMappingProtocol::__getitem__ needs to be implemented. To get O(1) __contains__, PySequenceProtocol::__contains__ needs to be implemented. To get proper Iteration support, PyIterProtocol::__iter__ needs to be implemented. PyO3/pyo3#611 This commit adds the correct implementation of the three traits to PyVocab.
In order to get arbitrary keys, PyMappingProtocol::__getitem__ needs to be implemented. To get O(1) __contains__, PySequenceProtocol::__contains__ needs to be implemented. To get proper Iteration support, PyIterProtocol::__iter__ needs to be implemented. PyO3/pyo3#611 This commit adds the correct implementation of the three traits to PyVocab.
The methods are not expected by CPython and are only explicitly callable. To get iteration support, PyIterProtocol should be implemented and to get support for `x in mapping`, PySequenceProtocol's __contains__ should be implemented. PyO3#611
The methods are not expected by CPython and are only explicitly callable. To get iteration support, PyIterProtocol should be implemented and to get support for `x in mapping`, PySequenceProtocol's __contains__ should be implemented. PyO3#611
In order to get arbitrary keys, PyMappingProtocol::__getitem__ needs to be implemented. To get O(1) __contains__, PySequenceProtocol::__contains__ needs to be implemented. To get proper Iteration support, PyIterProtocol::__iter__ needs to be implemented. PyO3/pyo3#611 This commit adds the correct implementation of the three traits to PyVocab.
In order to get arbitrary keys, PyMappingProtocol::__getitem__ needs to be implemented. To get O(1) __contains__, PySequenceProtocol::__contains__ needs to be implemented. To get proper Iteration support, PyIterProtocol::__iter__ needs to be implemented. PyO3/pyo3#611 This commit adds the correct implementation of the three traits to PyVocab.
In order to get arbitrary keys, PyMappingProtocol::__getitem__ needs to be implemented. To get O(1) __contains__, PySequenceProtocol::__contains__ needs to be implemented. To get proper Iteration support, PyIterProtocol::__iter__ needs to be implemented. PyO3/pyo3#611 This commit adds the correct implementation of the three traits to PyVocab.
Closed by #644. |
🐛 Bug Reports
When reporting a bug, please provide the following information. If this is not a bug report you can just discard this template.
🌍 Environment
rustc --version
): rustc 1.38.0-nightly (311376d30 2019-07-18)version = "0.x.y"
withgit = "https://github.com/PyO3/pyo3")?
yes💥 Reproducing
Implementing
PyIterProtocol
for a type implementingPyMappingProtocol
seems to overwrite__iter__
in Python. Invoking an iterator through any kind of statement callsPyIterProtocol::__iter__
.PyMappingProtocol::__iter__
is not accessible. Checking whether an element is inmapping
through thein
keyword runs an iterator and compares items instead of using__contains__
.__contains__
can be invoked explicitly throughPyMapping.__contains__
.If the implementation of
PyIterProtocol
is removed forPyMapping
, statements such as:fail by stating that
mapping
is not iteratable. Although, the iterator can be called explicitly throughPyMapping.__iter__
but usingiter()
on aPyMapping
object fails:Please also write what exact flags are required to reproduce your results.
The text was updated successfully, but these errors were encountered: