Great question! Let's walk through the PtrType class you've defined and explain what's going on, especially concerning other.points_to being reported as unknown or not a BaseType.
Quick Rundown of the Code
You define a generic class PtrType[T: BaseType], which:
- Inherits from
BaseType,
- Wraps another type
points_to which is an instance of T (some BaseType),
- Provides a representation of a pointer to that
points_to type.
Here's the key method for your question:
def accepts(self, other: BaseType) -> bool:
if isinstance(other, PtrType):
return self.points_to.accepts(other.points_to) # type: ignore
return False
You're checking if other is a PtrType, and then recursively checking if both points_to types are compatible.
So, why is other.points_to "unknown" or causing type issues?
A few potential reasons based on your setup:
🧩 1. Lack of Type Narrowing
You're passing in other: BaseType, so from the type system's perspective, other is not guaranteed to have points_to.
Even though you do check isinstance(other, PtrType), your typing annotation still says:
def accepts(self, other: BaseType) -> bool:
So the type checker (like MyPy or Pyright) doesn't know that other has the .points_to attribute unless you tell it explicitly.
✅ Fix: Add Type Narrowing via Type Hint
To let the type checker know other.points_to exists, you can use a type guard or explicit cast after the check. For example:
def accepts(self, other: BaseType) -> bool:
if isinstance(other, PtrType):
other_ptr = other # narrow the type
return self.points_to.accepts(other_ptr.points_to)
return False
Or with typing and cast if needed:
from typing import cast
def accepts(self, other: BaseType) -> bool:
if isinstance(other, PtrType):
other_ptr = cast(PtrType, other)
return self.points_to.accepts(other_ptr.points_to)
return False
The # type: ignore works by suppressing the warning but doesn't actually fix the root cause.
🧩 2. Your Generic Param Might Not Be Recognized as a BaseType Subclass
You're declaring:
class PtrType[T: BaseType = BaseType](BaseType):
This accepts a generic T constrained to be a BaseType. Good! But depending on how BaseType and T are implemented, the actual runtime object at other.points_to might not explicitly show up in the type system as a BaseType.
To improve type inference, especially if you're subclassing or working with complex generic hierarchies, make sure BaseType is using Protocol or abstract base classes in a way that plugins like MyPy or Pyright understand.
🧩 3. You May Need Custom __eq__ or accepts() on BaseType
Your pattern suggests you want different BaseType subclasses to implement the .accepts() method. Make sure that every possible points_to value used with PtrType is actually an instance of something that:
- Inherits from
BaseType,
- Implements
accepts().
If that's not consistent, you'll get runtime errors or unknown type behavior.
✅ TL;DR Fix Suggestions
-
Ensure type narrowing by moving other into a local variable after isinstance() check.
if isinstance(other, PtrType):
return self.points_to.accepts(other.points_to)
-
OR, annotate other with PtrType directly in function signature if always expected:
def accepts(self, other: PtrType) -> bool:
return self.points_to.accepts(other.points_to)
But this only works if the only valid other is also specific to PtrType.
-
Use cast from typing if you want to be explicit and safe.
Bonus 🧠
If you're using this for a language or compiler style IR representation (as your LLVM hints suggest), also consider using __eq__ or a visitor pattern for type checks – to better handle recursive structures like pointers to pointers.
If you share how BaseType is defined, I can be more exact in typing fixes!