mkleehammer / pyodbc

Python ODBC bridge
https://github.com/mkleehammer/pyodbc/wiki
MIT No Attribution
2.92k stars 561 forks source link

Wrong typings for the module #927

Closed dfrison01 closed 3 years ago

dfrison01 commented 3 years ago

Environment

Issue

As illustrated in the attached images:

I'm sorry if the description is poor since I don't have advanced knowledge in both the language and the module. I'm available to supply any other detail as needed. Thanks a lot for the attention!

pyodbc1 pyodbc2 pyodbc3

keitherskine commented 3 years ago

Hi @dfrison01 , I'm having some difficulty reproducing your issue. When I use Pylance and pylint to analyze a similar code snippet in VS Code, I get the following: image I do get an error of sorts on pyodbc.connect() but no issue with the __enter__ function. And I don't get any error on the sorted() function.

Are you perhaps using a linter different from pylint? Every linter has its own quirks.

It may help to look at the logs for Pylance. In VS Code, you can see those logs by opening up the terminal, selecting "OUTPUT" and choosing "Python Language Server" in the dropdown. You should see something like this: image You may need to set the log level to "Trace" to see more information in the logs. Here's part of my VS Code settings file:

    "python.analysis.logLevel": "Trace",
    "python.languageServer": "Pylance",
    "python.linting.enabled": true,
    "python.linting.pylintEnabled": true,

I hope this helps.

dfrison01 commented 3 years ago

Hey @keitherskine! Thank you for your time. Yes, I was also having the issue with "timeout" and "attrs_before" supposedly missing. All I did was edit the pyodbc.pyi (stub file for the module, on the site-packages folder if I recall correctly), setting a dummy default argument for both variables. After that, Pylance's next complaint will be just like my screenshots, about how the "Row" object doesn't implement "new" as Pylance would like it to (or something on that direction). That's where I'm stuck.

keitherskine commented 3 years ago

Ah, I see what you mean now, @dfrison01 . I see the errors you're getting. The issue is that there are a few "dunder" methods missing from the pyodbc.pyi file. I will raise a PR to add them in. Meanwhile, you can add those dunder methods manually to your .pyi file (as you have already done for the connect() function).

For the Row class, add the following methods:

    def __contains__(self, key, /) -> int: ...
    def __delattr__(self, name, /) -> None: ...
    def __delitem__(self, key, /) -> None: ...
    def __eq__(self, value, /) -> bool: ...
    def __ge__(self, value, /) -> bool: ...
    def __getattribute__(self, name, /) -> Any: ...
    def __getitem__(self, key, /) -> Any: ...
    def __gt__(self, value, /) -> bool: ...
    def __le__(self, value, /) -> bool: ...
    def __len__(self, /) -> int: ...
    def __lt__(self, value, /) -> bool: ...
    def __ne__(self, value, /) -> bool: ...
    def __reduce__(self) -> Any: ...
    def __repr__(self, /) -> str: ...
    def __setattr__(self, name, value, /) -> None: ...
    def __setitem__(self, key, value, /) -> None: ...

(actually, to fix the "sorted" issue, you need to add only the __lt__ method).

For the Cursor class, add:

    def __enter__(self) -> Cursor: ...
    def __exit__(self, exc_type, exc_value, traceback) -> None: ...
    def __iter__(self, /) -> Cursor: ...
    def __next__(self, /) -> Row: ...

For the Connection class, add:

    def __enter__(self) -> Connection: ...
    def __exit__(self, exc_type, exc_value, traceback) -> None: ...

After that, you shouldn't have any more Pylance type hints errors, but if you do, let me know.

dfrison01 commented 3 years ago

@keitherskine That seem to be it, worked for me. Thank you very much!