Python 3.11+ Features in NOW-LMS
NOW-LMS requires Python 3.11+ and takes advantage of modern Python language features for better code quality, performance, and maintainability.
Implemented Features
Python 3.8 Features
1. Positional-Only Parameters (/
)
Location: now_lms/auth.py
Used in critical authentication functions to prevent keyword argument misuse:
def proteger_passwd(clave, /):
"""Protege contraseña con argon2. Solo acepta argumentos posicionales."""
def validar_acceso(usuario_id, acceso, /):
"""Verifica inicio de sesión. Solo acepta argumentos posicionales."""
Benefits: - Prevents accidental keyword argument usage in security-critical functions - Makes API intentions clearer - Protects against future parameter name changes
Python 3.9 Features
1. Native Type Hinting for Generics
Locations: now_lms/misc.py
, now_lms/mail.py
Replaced old-style Union
types with modern union operator:
# Before
from typing import Union
def func(param: Union[str, None]) -> Union[dict, None]:
# After
def func(param: str | None) -> dict | None:
Benefits: - More readable type hints - Consistent with modern Python style - Better IDE support
Python 3.10 Features
1. Structural Pattern Matching (match
)
Location: now_lms/vistas/home.py
Replaced long if-elif chains with pattern matching for user role handling:
# Before
if current_user.tipo == "admin":
# admin logic
elif current_user.tipo == "student":
# student logic
elif current_user.tipo == "instructor":
# instructor logic
else:
# default
# After
match current_user.tipo:
case "admin":
# admin logic
case "student":
# student logic
case "instructor":
# instructor logic
case _:
# default
Location: now_lms/mail.py
String-to-boolean conversion:
# Before
if MAIL_USE_SSL == "FALSE":
MAIL_USE_SSL = False
elif MAIL_USE_SSL == "TRUE":
MAIL_USE_SSL = True
# After
match MAIL_USE_SSL:
case "FALSE":
MAIL_USE_SSL = False
case "TRUE":
MAIL_USE_SSL = True
Benefits: - More readable than long if-elif chains - Pattern matching is more powerful and extensible - Better performance for multiple comparisons
Python 3.11 Features
1. Improved Typing with Self
Location: now_lms/db/__init__.py
Used Self
return type annotation for methods that return the instance:
from typing import Self
class ForoMensaje(database.Model, BaseTabla):
def get_thread_root(self) -> Self:
"""Retorna el mensaje raíz del hilo."""
if self.parent_id:
return self.parent.get_thread_root()
return self
Benefits: - More accurate type hints for methods returning self - Better IDE support and type checking - Self-documenting code
Code Quality Impact
Linting and Formatting
All modernized code passes: - Black: Python code formatter with line length 127 - Flake8: Linting with project-specific rules - mypy: Type checking (when available) - ruff: Fast Python linter
Testing
All features are validated by: - Unit tests continue to pass - Integration tests validate functionality - Route testing ensures no regressions
Future Opportunities
Additional Python 3.8+ Features to Consider
- Assignment Expressions (Walrus Operator): Look for file processing and validation patterns where assignment+check is common
- More Positional-Only Parameters: Apply to other core functions like database operations
- Exception Groups: For handling multiple concurrent errors in file uploads or batch operations
- Parenthesized Context Managers: For file operations and database transactions
Performance Benefits
- Pattern Matching: Better performance than if-elif chains for multiple comparisons
- Native Type Hints: Reduced import overhead
- Walrus Operator: Eliminates redundant computations
Migration Guidelines
When modernizing additional code:
- Prioritize Safety: Always test changes thoroughly
- Focus on Readability: Only apply features where they improve code clarity
- Maintain Compatibility: Ensure all team members use Python 3.11+
- Document Changes: Update this file when adding new features