Skip to content

Instantly share code, notes, and snippets.

@mypy-play
Created December 27, 2025 18:53
Show Gist options
  • Select an option

  • Save mypy-play/8fbe0316234691cdc2f8e85e324d56e9 to your computer and use it in GitHub Desktop.

Select an option

Save mypy-play/8fbe0316234691cdc2f8e85e324d56e9 to your computer and use it in GitHub Desktop.
Shared via mypy Playground
from pathlib import Path
class MyClass:
def __init__(self, path: Path | None) -> None:
self.path = path
def test(a: MyClass, b: MyClass):
if a.path is None:
raise ValueError(f"a.path cannot be None")
if b.path is None:
raise ValueError(f"b.path cannot be None")
# This line will cause an error for the line "b.path.exists()": error: Item "None" of "Path | None" has no attribute "exists" [union-attr]
b = a
if a.path.exists():
print("a path exist")
if b.path.exists():
print("b path exist")
def main():
a = MyClass(Path("."))
b = MyClass(Path("."))
test(a, b)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment