Skip to content

PyMapping is inconsistent with typing.Mapping #2072

@Gobot1234

Description

@Gobot1234

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions