reportTypedDictNotRequiredAccess.md
April 8, 2026 · View on GitHub
Overview
reportTypedDictNotRequiredAccess flags cases where you access a non-required (optional) field of a TypedDict without first checking if it is present. This diagnostic helps prevent runtime errors and ensures safe access to optional fields in typed dictionaries.
Representative Issues
- #4163: Ensure consistency in the use of type stubs between Pyright's CLI and Pylance settings.
- #5200: Provide a configuration setting to allow users to customize diagnostic rule severities based on the type checking mode.
- #1693: Use type inheritance in TypedDict to clearly define required and not required fields.
- #4173: Implement per-module configuration settings in Pyright for more flexible type checking.
Examples
Error:
from typing import TypedDict, NotRequired
class UserProfile(TypedDict):
name: str
nickname: NotRequired[str]
def greet(profile: UserProfile) -> str:
return f"Hi, {profile['nickname']}" # 'nickname' may not exist
Fix — check for the key first:
def greet(profile: UserProfile) -> str:
nick = profile.get("nickname", profile["name"])
return f"Hi, {nick}"
Or use in to guard access:
def greet(profile: UserProfile) -> str:
if "nickname" in profile:
return f"Hi, {profile['nickname']}"
return f"Hi, {profile['name']}"
Common Fixes & Workarounds
- Check for the presence of a non-required field in a
TypedDictbefore accessing it (e.g., usingif "field" in my_dict:). - Use type inheritance to clearly define required and not required fields in your
TypedDictdefinitions. - Use per-module configuration settings to adjust diagnostics as needed.
- Review the Pyright configuration documentation for details on configuring or disabling this diagnostic.
See Also
python.analysis.diagnosticSeverityOverrides— adjust or suppress this diagnosticpython.analysis.typeCheckingMode— controls which diagnostics are enabled by default