electronstudio / raylib-python-cffi

Python CFFI bindings for Raylib
http://electronstudio.github.io/raylib-python-cffi
Eclipse Public License 2.0
142 stars 29 forks source link

default colors introduced as literal tuples #129

Open UrieW opened 3 weeks ago

UrieW commented 3 weeks ago

I was trying a Hello World example, until when it came to clear_background(), it won't run because the default colors are recognized as tuples with literal values instead of color classes.

I had strict type checking on while doing this. Please fix!!!!

electronstudio commented 2 weeks ago

I wasn't aware of any implementation of Python that has static type checking built in. Can you please provide detailed complete step by step instructions on how to reproduce this error? Include the full output of the error message.

Type hints are provided for the pyray module to assist IDE autocompletion, but if you try to run them through a static type checker like mypy you will encounter other problems. (For example, there is no such thing as the Color class. There's a type hint for it so you know when you're expected to supply a color tuple, but the class itself doesn't exist because it's not a Python class, it's a C struct.)

electronstudio commented 2 weeks ago

I think we could add fake type hints to the colours to make them look like Color classes for the purpose of reducing the warnings produced by static type checkers. However I'm not convinced that changing the actual type is a good idea, unless there is evidence of the tuple type causing errors.

The colours in Raylib are C 'define' macros so there is no actual equivalent possible in Python. (Python CFFI only supports int 'defines' I think.) So the choice is either tuples, which is what we've had for years with no problems, or else change them to pointers to ctype structs. The structs might improve performance, but they might reduce performance, and the change could break people's programs.

UrieW commented 2 weeks ago
  1. Install Vscode, all python extensions and python
  2. Install raylib by pip(it's exactly called raylib in the command line
  3. Create a Hello World project using raylib
  4. Turn VSCode's types checking to "strict" in settings.json
  5. Run it after completing the project
  6. Look at the line giving the error
UrieW commented 2 weeks ago

Also, I use the Color class from it just fine. It just won't take tuples. The default colors are read as tuples and not read as Colors.

electronstudio commented 2 weeks ago
  1. Install Vscode, all python extensions and python

    1. Install raylib by pip(it's exactly called raylib in the command line

    2. Create a Hello World project using raylib

    3. Turn VSCode's types checking to "strict" in settings.json

    4. Run it after completing the project

    5. Look at the line giving the error

I followed your directions. VSCode underlines the type inconsistencies, but the program still runs fine, so I'm unable to reproduce. You'll have to make a video or something.

Regardless, even if there is a way to make VSCode fail to run on type inconsistencies, the solution is going to be "dont enable that". The solution I proposed above will probably stop it complaining about the Color type, but there's hundreds of others in the raylib package it won't like. To repeat again, the type hints are not provided for the purpose of running them through a type checker.

Also, I use the Color class from it just fine. It just won't take tuples. The default colors are read as tuples and not read as Colors.

To repeat yet again. There is no Color class. It's a fake type hint. When you call pyray.Color() it's a generator function made to mimic the look of a class constructor.

electronstudio commented 2 weeks ago

Btw if you don’t want want the (fake) type hints at all you can delete the file pyray.pyi. The library will work fine without them and you won’t get warnings. (Unless you have your type checker set to require everything to be typed, I guess)

electronstudio commented 2 weeks ago

Typehints for the colours https://github.com/electronstudio/raylib-python-cffi/pull/131