-
Notifications
You must be signed in to change notification settings - Fork 768
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
Support for declaring classes in try/except #4691
Comments
You're providing two different (incompatible) type declarations for the same symbol in the same scope. This breaks a fundamental rule of a static type checker. If you don't want to use static type checking, you can disable these errors by setting If you want to leave static type checking enabled, you'll need to provide a workaround in your code.
CIRCUIT_PYTHON = False # Replace with appropriate check
if CIRCUIT_PYTHON:
class BaseClass(object):
pass
else:
from typing import TypeVar, Generic
T = TypeVar("T")
class BaseClass(Generic[T]):
pass In your pyproject.toml file: [tool.pyright]
defineConstant = { CIRCUIT_PYTHON = false }
For more information, please refer to pyright's documentation; |
It seems my example wasn't quite complete enough. While your suggestion was helpful, it's not quite the complete example for my situation. My current MCVE is this: CIRCUIT_PYTHON = False
try:
from typing import TypeVar, Generic
CIRCUIT_PYTHON = False
except ImportError:
CIRCUIT_PYTHON = True
if CIRCUIT_PYTHON:
T = object
class BaseClass(object):
pass
else:
T = TypeVar('T')
class BaseClass(Generic[T]):
pass
class GenericExampleClass(BaseClass[T]):
placeholder_var: T This fails at runtime with the error: The class I've renamed to EDIT: Oh, and I also posted on Adafruit's support forums with no response yet. |
Hi @proegssilb, this construct isn't supported in CircuitPython: class GenericExampleClass(BaseClass[T]):
placeholder_var: T so you'll have to declare the class GenericExampleClass(BaseClass):
placeholder_var: object If you want type checking just when using Pylance, you can do something like this: from typing import TYPE_CHECKING
CIRCUIT_PYTHON = False
try:
from typing import TypeVar, Generic
CIRCUIT_PYTHON = False
except ImportError:
CIRCUIT_PYTHON = True
if not TYPE_CHECKING:
T = object
class BaseClass(object):
pass
else:
T = TypeVar('T')
class BaseClass(Generic[T]):
pass
class GenericExampleClass(BaseClass):
placeholder_var: object |
By inheriting from Further, the entire But, interesting approach with making |
In case anyone finds this in the future, what I actually do is manually maintain a |
Due to CircuitPython intentionally not supporting the
typing
module, you instead have to get creative:However, when I do this, Pylance registers two errors:
(The line & column number here point to the
except
clause)The second error:
Note that BaseClass is indeed a base class.
You can get only the second error if you do the following:
Regardless of whether or not this code is ideal, is there a way we could make this code work a little more nicely?
The text was updated successfully, but these errors were encountered: