Skip to content
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

datetime.date isinstance PyDateTime returns True? #3069

Closed
MarcoGorelli opened this issue Mar 27, 2023 · 0 comments · Fixed by #3071
Closed

datetime.date isinstance PyDateTime returns True? #3069

MarcoGorelli opened this issue Mar 27, 2023 · 0 comments · Fixed by #3071
Labels

Comments

@MarcoGorelli
Copy link

Bug Description

Given the example below, I would have expected

my_date.is_instance_of::<PyDateTime>().unwrap()

to be false

Steps to Reproduce

Here's my main.rs file:

use pyo3::prelude::*;
use pyo3::types::{PyDateTime, PyDate};

fn main() -> () {
    Python::with_gil(|py| {
        let datetime = py.import("datetime").unwrap();
        let my_datetime = datetime.call_method1("datetime", (2022, 3, 28, 0, 0, 0, 0)).unwrap();
        println!("isinstance pydatetime: {:?}", my_datetime.is_instance_of::<PyDateTime>().unwrap());
        println!("isinstance pydate: {:?}", my_datetime.is_instance_of::<PyDate>().unwrap());

        let my_date = datetime.call_method1("date", (2022, 3, 28)).unwrap();
        println!("isinstance pydatetime: {:?}", my_date.is_instance_of::<PyDateTime>().unwrap());
        println!("isinstance pydate: {:?}", my_date.is_instance_of::<PyDate>().unwrap());
    })
}

Here's my Cargo.toml file:

[package]
name = "tmp"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
pyo3 = { version = "0.18.0", features = ["auto-initialize"] }

If I run the above, I get

isinstance pydatetime: true
isinstance pydate: true
isinstance pydatetime: true
isinstance pydate: true

I find this a bit odd, because running in Python directly, I get:

In [27]: import datetime as dt

In [28]: isinstance(my_datetime, dt.datetime)
Out[28]: True

In [29]: isinstance(my_datetime, dt.date)
Out[29]: True

In [30]: isinstance(my_date, dt.datetime)
Out[30]: False

In [31]: isinstance(my_date, dt.date)
Out[31]: True

Backtrace

No response

Your operating system and version

Linux DESKTOP-U8OKFP3 5.10.102.1-microsoft-standard-WSL2 #1 SMP Wed Mar 2 00:30:59 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

Your Python version (python --version)

Python 3.8.16

Your Rust version (rustc --version)

rustc 1.68.1 (8460ca823 2023-03-20)

Your PyO3 version

0.18.0

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

virtualenv

Additional Info

No response

sollyucko added a commit to sollyucko/pyo3 that referenced this issue Mar 27, 2023
bors bot added a commit that referenced this issue Mar 28, 2023
3071: fix typo DateType -> DateTimeType, fixes #3069 r=adamreichold a=sollyucko

This should fix #3069 AKA https://stackoverflow.com/questions/75857300/tell-if-object-isinstance-of-pydatetime-or-pydate. I've added an entry in newsfragments and I've added some regression tests in test_datetime.rs.

Co-authored-by: Solomon Ucko <[email protected]>
@bors bors bot closed this as completed in 7e2f732 Mar 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant