pyo3: PyMapping is inconsistent with typing.Mapping

Bug Description

The conversion table for PyMapping suggests anything that is a typing.Mapping should be able to be converted to PyMapping, this however is not the case seemly list and str are also downcast-able to a PyMapping.

This causes errors when you try to access things like items on the PyMapping as you get a AttributeError str has no attribute items.

Steps to Reproduce

use pyo3::prelude::*;
use pyo3::types::PyMapping;

#[pyfunction]
fn downcast_(a: &PyAny) -> PyResult<&PyMapping> {
    Ok(a.downcast::<PyMapping>()?)
}

#[pymodule]
fn downcast(_py: Python, m: &PyModule) -> PyResult<()> {
    m.add_function(wrap_pyfunction!(downcast_, m)?)?;

    Ok(())
}
>>> import typing, downcast
>>> isinstance({}, typing.Mapping)
True
>>> downcast.downcast_({})
{}
>>> isinstance("", typing.Mapping)
False
>>> downcast.downcast_("")
''

Backtrace

No response

Your operating system and version

MacOS 11.1 (20C69)

Your Python version (python --version)

3.9.3

Your Rust version (rustc --version)

rustc 1.53.0-nightly (07e0e2ec2 2021-03-24)

Your PyO3 version

0.15.1

How did you install python? Did you use a virtualenv?

venv

Additional Info

No response

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 28 (26 by maintainers)

Most upvoted comments

@aganders3 if you’re willing to pick this up, would be very grateful. I think the only difference to my above proposal is that the fast path checking the flags would only apply on 3.10 and up when not(Py_LIMITED_API).

Thanks so much for making me aware. I hadn’t seen this because 1) my tests for mappings were not good enough 🙈 2) I trusted pyo3 too much.

Should be partially fixed in https://github.com/samuelcolvin/pydantic-core/pull/117

I would agree that your suggestion https://github.com/PyO3/pyo3/issues/2072#issuecomment-999952382 of the fast solution for 3.10 and falling back to python isinstance for <=3.9, seems like the best route.

Hmm maybe for now you can just call collections.abc.Mapping.register() (using PyModule::import) by hand in the test before trying any mapping checks?

Agreed #991 is the proper solution, I hope that progress can be made on that eventually…

I trusted pyo3 too much.

Hopefully you can continue to trust PyO3! This is the only huge footgun of a bug which I’m aware of 🤪

Considering this is a wont-fix upstream, could we just implment Davids suggestion to fix this?

I’m +1 on this and would be happy to implement as well if no one else wants to claim it.

To be clear I think the proposal was:

  • check the tpflags for Python >=3.10
  • fall back to a call into Python to do an isintance check for Python <=3.9

I wouldn’t call it blocked. I’m still working on a proposal to send to sig-capi and in the issue.

I found some seemingly relevant discussion on BPO https://bugs.python.org/issue5945

One suggestion there is to use PyMapping_Check() && !PySequence_Check() for a true mapping check. Would that work here?

I think this is awkward enough of a footgun (and also new enough, having been added in 0.15) that it would be good if we could come to a decision about what to do for this in time for the 0.16 release.

All opinions very much wanted!

I like the sound of using both the tp flags and the instance checks depending on your version