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

[BUG] not None semantics is not supported in Pure Python mode for object type #5624

Closed
matusvalo opened this issue Aug 15, 2023 · 4 comments
Closed

Comments

@matusvalo
Copy link
Contributor

Describe the bug

Compiling reproducer is run without error. This is inconsistent logic used in Python Objects [1].

[1] https://cython.readthedocs.io/en/latest/src/userguide/extension_types.html#extension-types-and-none

Code to reproduce the behaviour:

import typing
import cython

def bar(i: typing.Optional[object]):
    pass

def bar2(i: object):
    pass

bar(None)
bar([1])
bar2(None)    # This line is executed without error

Expected behaviour

Line bar2(None) should raise following exception:

TypeError: Argument 'i' must not be None

OS

any

Python version

3.9

Cython version

master

Additional context

Cython version:

import cython

def bar(object i):
    pass

def bar2(object i not None):
    pass

bar(None)
bar([1])
bar2(None)   # Here is raised TypeError

Related to #5612

@da-woods
Copy link
Contributor

I don't agree with this (or #5625).

From Cython's point of view, the value of not None is to be able to make optimizations by directly accessing an extension type/built-in type/memoryview without having to check if it's None. There's really no optimizations to be done for object so there's no value in this. The point isn't really to enforce user's type annotations - it's just to make efficient use of them.

I'd also argue that None is an object, so should be accepted for an object/untyped argument.

I'm not sure if non-pure mode accepts object not None. I guess it probably does. But that's probably just an accident more than a useful design.

The documentation looks to be in terms of "extension or Python builtin types". I don't really consider object to be one of those, so I don't think it's inconsistent. But we could clarify it.

@matusvalo
Copy link
Contributor Author

I don't agree with this (or #5625).

This is fine. I am not 100% sure whether those are real issues (see description). I created the issues because it make sense from consistency stand point - where not None is supported in Cython, I expected that also pure python mode has similar behavior.

Feel free to close the issues without asking.

I'm not sure if non-pure mode accepts object not None.

Cython languages accepts not None in both issues as presented in examples (unless I did a stupid mistake)

@scoder
Copy link
Contributor

scoder commented Aug 15, 2023 via email

@matusvalo
Copy link
Contributor Author

Based on the comments I am closing the Issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants