Close Menu
    Facebook X (Twitter) Instagram
    Articles Stock
    • Home
    • Technology
    • AI
    • Pages
      • About ArticlesStock — AI & Technology Journalist
      • Contact us
      • Disclaimer For Articles Stock
      • Privacy Policy
      • Terms and Conditions
    Facebook X (Twitter) Instagram
    Articles Stock
    AI

    A Coding Implementation on Pyright Sort Checking Protecting Generics, Protocols, Strict Mode, Sort Narrowing, and Trendy Python Typing

    Naveed AhmadBy Naveed Ahmad30/04/2026Updated:30/04/2026No Comments2 Mins Read
    blog 2 7


    print("=" * 62)
    print("SECTION 9 · pyrightconfig.json")
    print("=" * 62)
    
    
    config = {
       "embrace": ["src"],
       "exclude": ["**/__pycache__"],
       "pythonVersion": "3.11",
       "typeCheckingMode": "strict",
       "reportMissingImports": "error",
       "reportMissingTypeStubs": "warning",
       "reportUnknownVariableType": "warning",
       "reportUnknownMemberType": "warning",
       "reportUnnecessaryTypeIgnoreComment": "warning",
    }
    cfg_path = os.path.be part of(WORK, "pyrightconfig.json")
    with open(cfg_path, "w") as f:
       json.dump(config, f, indent=2)
    print(f"Written: {cfg_path}")
    print(json.dumps(config, indent=2))
    print()
    
    
    print("=" * 62)
    print("SECTION 10 · Self, TypeAlias, NewType")
    print("=" * 62)
    
    
    write("s10_modern_types.py", """
       from typing import Self, TypeAlias, NewType
    
    
       class Question:
           def __init__(self) -> None:
               self._filters: checklist[str] = []
    
    
           def the place(self, cond: str) -> Self:
               self._filters.append(cond)
               return self
    
    
           def construct(self) -> str:
               return " AND ".be part of(self._filters)
    
    
       class AdvancedQuery(Question):
           def order_by(self, col: str) -> Self:
               return self
    
    
       q = AdvancedQuery().the place("age > 18").order_by("title")
       reveal_type(q)
    
    
       Vector: TypeAlias = checklist[float]
       Matrix: TypeAlias = checklist[Vector]
    
    
       def dot(a: Vector, b: Vector) -> float:
           return sum(x * y for x, y in zip(a, b))
    
    
       v1: Vector = [1.0, 2.0, 3.0]
       v2: Vector = [4.0, 5.0, 6.0]
       dot(v1, v2)
       dot(v1, [1, 2, 3])
    
    
       UserId   = NewType("UserId", int)
       OrderId  = NewType("OrderId", int)
    
    
       def get_user(uid: UserId) -> str:
           return f"user_{uid}"
    
    
       uid = UserId(42)
       oid = OrderId(99)
    
    
       get_user(uid)
       get_user(oid)
       get_user(42)
    """)
    
    
    print("→ s10_modern_types.py:")
    run_pyright("s10_modern_types.py")
    
    
    print("=" * 62)
    print("SECTION 11 · reveal_type() & kind: ignore")
    print("=" * 62)
    
    
    write("s11_reveal_ignore.py", """
       from typing import Any
    
    
       values = [1, "two", 3.0]
       reveal_type(values)
    
    
       def thriller(x: Any) -> Any:
           return x
    
    
       r = thriller(42)
       reveal_type(r)
    
    
       unhealthy: int = "oops"
       bad2: int = "additionally unhealthy"  # kind: ignore[assignment]
    """)
    
    
    print("→ s11_reveal_ignore.py:")
    run_pyright("s11_reveal_ignore.py")
    
    
    print("=" * 62)
    print("TUTORIAL COMPLETE")
    print("=" * 62)
    print("""
    Subjects coated
    ──────────────
    1  Primary annotations & inference
    2  Optionally available / Union / PEP 604 syntax
    3  Sort narrowing (isinstance, guards, TypeGuard, match)
    4  Generics — TypeVar, Generic, ParamSpec
    5  Protocols & structural subtyping
    6  TypedDict, dataclasses, NamedTuple
    7  Literal, Last, @overload
    8  Strict mode
    9  pyrightconfig.json
    10  Self, TypeAlias, NewType
    11  reveal_type() & kind: ignore
    
    
    All supply information written to: /tmp/pyright_tutorial/
    """)



    Source link

    Naveed Ahmad

    Naveed Ahmad is a technology journalist and AI writer at ArticlesStock, covering artificial intelligence, machine learning, and emerging tech policy. Read his latest articles.

    Related Posts

    Salesforce is crowdsourcing its AI roadmap — with prospects 

    30/04/2026

    Spotify introduces verified artist badges to assist distinguish people from AI

    30/04/2026

    Netflix desires you to observe ‘Clips,’ its TikTok-like vertical video feed

    30/04/2026
    Leave A Reply Cancel Reply

    Categories
    • AI
    Recent Comments
      Facebook X (Twitter) Instagram Pinterest
      © 2026 ThemeSphere. Designed by ThemeSphere.

      Type above and press Enter to search. Press Esc to cancel.