Things that does NOT work
The following are my failed attempts to get Python to act more intelligently. But none worked 😔.
Getting IntelliSense in your IDE by writing Docstring, or using Pydantic lib
```py from pydantic import BaseModel, Field from typing import Annotated, Optional class Person(BaseModel): """A person with contact information. Attributes: name: Full name of the person. email: Email address. age: Age in years. """ name: str = Field(..., description="Full name. Example: Alice Johnson") email: str = Field(..., description="Email address. Example: alice@example.com") age: Optional[int] = None class Person(BaseModel): """ A person with contact information. Attributes: name (str): Full name of the person. email (str): Email address of the person. age (Optional[int]): Age in years. Must be between 0 and 150. """ name: str email: str age: Optional[int] = None class Person(BaseModel): """ A person with contact information. """ name: Annotated[str, "Fullname of the person. Examples: Alice Johnson, Bob Lee"] email: Annotated[str, "Email address. Must be valid format. Examples: john@example.com"] age: Optional[int] = Field(None) class Person(BaseModel): """ A person with contact information. """ name: str = Field( description="Full name of the person.", examples=["Alice Johnson", "Bob Lee"] ) email: str = Field( description="Email address. Must be valid format.", examples=["john@example.com"] ) age: Optional[int] = Field( None, description="Age in years. Must be between 0 and 150.", examples=[25] ) person = Person(name="Test", email="test@example.com", age=30) print(person.name) ```Type Hinting Techniques in Python
I love to have types, because they gimme a sense of what the heck am I doing and what I should not probably do :grin:.
Creating type aliases with Literal values for better type safety, and still you an pass other strings that were not listed in the Literal
```py
from typing import TypeAlias, Literal
Role: TypeAlias = Literal["admin", "guest", "moderator"] | str
def some_func(role: Role):
pass
some_func("")
```
Docstring for class properties
```py class Session: """ A session stores configuration state and allows you to create service clients and resources. :type aws_access_key_id: string :param aws_access_key_id: AWS access key ID :type aws_secret_access_key: string :param aws_secret_access_key: AWS secret access key :type aws_session_token: string :param aws_session_token: AWS temporary session token :type region_name: string :param region_name: Default region when creating new connections :type botocore_session: botocore.session.Session :param botocore_session: Use this Botocore session instead of creating a new default one. :type profile_name: string :param profile_name: The name of a profile to use. If not given, then the default profile is used. """ def __init__( self, aws_access_key_id=None, aws_secret_access_key=None, aws_session_token=None, region_name=None, botocore_session=None, profile_name=None, ): pass ```How to define an interface which will accept any class as long as it has field x of type y?
```py from typing import Protocol, Any class EventPayload(Protocol): id: str body: Any # ... ```How to annotate tuple of tuples?
The `...` (ellipsis) means it can have any number of these inner tuples (including zero). ```py metadata: tuple[tuple[str, str], ...] = ( ("request-id", "83c5952d-ce66-4c1b-97cd-d732983e9cf5"), ("authorization", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImlhdCI6MTUxNjIzOTAyMn0.KMUFsIDTnFmyG3nMiGM6H9FNFUROf3wh7SmqJp-QV30"), ) ```Change the return type based on a parameter in Python
| No default value | With default value |
|---|---|
| ```py from typing import overload, Literal, Any, TypedDict class User(TypedDict): id: str name: str class UserRepository: @overload def update_user( self, filters: Any, return_update_users: Literal[True] ) -> list[User]: ... @overload def update_user( self, filters: Any, return_update_users: Literal[False] ) -> None: ... def update_user( self, filters: Any, return_update_users: bool ) -> list[User] | None: # TODO: update users! if not return_update_users: return # ... return [] userRepository = UserRepository() users = userRepository.update_user({}, True) result = userRepository.update_user({}, False) # Verify the types and values print(f"users: {users}, type: {type(users)}") print(f"result: {result}, type: {type(result)}") ``` | ```py from typing import overload, Literal, Any, TypedDict class User(TypedDict): id: str name: str class UserRepository: @overload def update_user( self, filters: Any, return_update_users: Literal[True] = True ) -> list[User]: ... @overload def update_user( self, filters: Any, return_update_users: Literal[False] ) -> None: ... def update_user( self, filters: Any, return_update_users: bool = True ) -> list[User] | None: # TODO: update users! if not return_update_users: return # ... return [] userRepository = UserRepository() users = userRepository.update_user({}, True) result = userRepository.update_user({}, False) # Verify the types and values print(f"users: {users}, type: {type(users)}") print(f"result: {result}, type: {type(result)}") ``` |