python-oracledb icon indicating copy to clipboard operation
python-oracledb copied to clipboard

ConnectionPool.acquire instance method returns type checking errors

Open troyswanson opened this issue 3 years ago • 6 comments

1. What versions are you using?

Oracle 19c

platform.platform: macOS-11.6.8-x86_64-i386-64bit
sys.maxsize > 2**32: True
platform.python_version: 3.10.6

oracledb.__version__: 1.0.3

2. Is it an error or a hang or a crash?

None; it is a type checking error

3. What error(s) or behavior you are seeing?

When calling the instance method ConnectionPool.acquire using the with statement, type checking tools report errors:

  • "Type[Connection]" has no attribute "__enter__"
  • "Type[Connection]" has no attribute "__exit__"

4. Does your application call init_oracle_client()?

N/A

5. Include a runnable Python script that shows the problem.

from oracledb import pool

dbpool: pool.ConnectionPool = pool.create_pool(
    user="username",
    password="password",
    dsn="dsn"
)

with dbpool.acquire() as conn:  # type checking error shows here
    conn.ping()  # works as expected, but conn is Unknown in type checkers

troyswanson avatar Aug 30 '22 14:08 troyswanson

I believe the issue is from the method signature returning Type[Connection] instead of just Connection here:

https://github.com/oracle/python-oracledb/blob/12a935a20f606207a516c6f76453d6b6805b6c66/src/oracledb/pool.py#L115-L123

From what understand, using Type[] signals to the type checker that the method is returning an uninstantiated class. This method returns an instantiated object, so the type it is returning would just be Connection.

If there is consensus in the group that this is the case, I would be happy to open a PR to patch this.

troyswanson avatar Aug 30 '22 15:08 troyswanson

As I understand it, this syntax is required for Python 3.6 support -- which is less capable. What tool are you using to give you the type checking errors? If you are able to test with both Python 3.6 and 3.10 and confirm that your patch works in both cases I would be happy to accept it!

anthony-tuininga avatar Aug 30 '22 17:08 anthony-tuininga

Here are the docs for 3.6 (which are the same as 3.10):

A variable annotated with C may accept a value of type C. In contrast, a variable annotated with Type[C] may accept values that are classes themselves – specifically, it will accept the class object of C.

ref https://docs.python.org/3.6/library/typing.html#typing.Type

What tool are you using to give you the type checking errors?

I was using Pyright and MyPy -- they both returned the same error.

If you are able to test with both Python 3.6 and 3.10 and confirm that your patch works in both cases I would be happy to accept it!

I will give it a shot and report back! o7

troyswanson avatar Aug 30 '22 18:08 troyswanson

I just did a pretty basic test using a Docker container in Python 3.6 and it behaved the same way as 3.10.

Making the change to that module also fixed it, so I'm going to make a MR.

troyswanson avatar Aug 30 '22 19:08 troyswanson

This issue also applies to the drop and release methods in the ConnectionPool class. I made a pretty basic PR that fixes, but I am not an authorized contributor, so you won't be able to merge that exact commit.

The maintainers of this project are welcome to use it as inspiration, though!

troyswanson avatar Aug 30 '22 20:08 troyswanson

Thanks, @troyswanson, for the report and for the suggested fix! We will take a look at this and integrate the changes internally after some testing.

anthony-tuininga avatar Aug 30 '22 20:08 anthony-tuininga

Included in python-oracledb 1.1.1 which was just released.

anthony-tuininga avatar Sep 29 '22 00:09 anthony-tuininga