Azure AI ML Code Review
Reviews uncommitted changes (staged and unstaged files) in the azure-ai-ml package, focusing on Azure SDK Python design guidelines, type safety, testing patterns, and API consistency.
Default Review Scope
Unless otherwise specified, review all uncommitted changes in the current branch (staged and unstaged files) within sdk/ml/azure-ai-ml/. This includes new files, modified files, and any pending changes that haven't been committed yet.
Review Focus Areas
1. Azure SDK Design Guidelines Compliance
- Check: Adherence to Azure SDK Python Design Guidelines
- Look for: Proper client naming, method patterns, parameter order
- Flag: Non-compliant naming (use
create_or_update not create_or_replace)
- Example Issue:
def get_job(name, subscription_id) should be def get_job(name, **kwargs)
Key Patterns:
- Client methods:
begin_* for LROs, list_* for paginators
- Naming: snake_case for methods, PascalCase for classes
- Parameters: Required positional, optional keyword-only
- Return types: Explicit type hints for all public APIs
2. Type Annotations & MyPy Compliance
- Check: Complete type annotations on all public APIs
- Look for: Proper use of
Optional, Union, TYPE_CHECKING
- Flag: Missing return types,
Any without justification, bare dict/list
- Example Issue:
def process_data(data) should be def process_data(data: Dict[str, Any]) -> ProcessedData
Common Fixes:
# Bad
def get_config(name):
return config
# Good
def get_config(name: str) -> Optional[Dict[str, Any]]:
return config
3. Pylint Compliance
- Check: Code passes pylint with azure-sdk-for-python rules
- Look for: Proper docstrings, no unused imports, correct argument names
- Flag: Violations of naming conventions, too many arguments (>5), long lines (>120)
- Reference: Azure Pylint Guidelines
Watch for:
client-method-should-not-use-static-method
missing-client-constructor-parameter-credential
client-method-has-more-than-5-positional-arguments
4. Async/Await Patterns
- Check: Proper async implementation in
_async modules
- Look for: Using
async with for clients, awaiting coroutines correctly
- Flag: Blocking calls in async code, missing
await, sync code in async modules
- Example Issue:
self._client.get() in async should be await self._client.get()
Pattern:
# In azure/ai/ml/aio/operations/
async def create_or_update(
self,
entity: Job,
**kwargs: Any
) -> Job:
async with self._lock:
result = await self._service_client.create_or_update(...)
return result
5. Error Handling & Validation
- Check: Proper exception handling with Azure SDK exceptions
- Look for: Use of
HttpResponseError, ResourceNotFoundError, proper validation
- Flag: Bare
except:, catching Exception without re-raising, missing validation
- Example Issue: Missing parameter validation before API calls
Pattern:
from azure.core.exceptions import ResourceNotFoundError, HttpResponseError
try:
result = self._operation.get(name)
except ResourceNotFoundError:
raise ResourceNotFoundError(f"Job '{name}' not found")
except HttpResponseError as e:
raise HttpResponseError(f"Failed to retrieve job: {e.message}")
6. API Design & Operations
- Check: Consistent CRUD patterns across operations classes
- Look for: Proper separation of sync/async, operations returning correct types
- Flag: Business logic in client, missing operations class, inconsistent method names
Structure:
azure/ai/ml/
├── operations/ # Sync operations
│ ├── job_operations.py
│ └── model_operations.py
└── aio/operations/ # Async operations (mirror structure)
├── job_operations.py
└── model_operations.py
7. Entity & Schema Patterns
- Check: Proper use of marshmallow schemas, correct entity inheritance
- Look for: Schema validation, proper serialization/deserialization
- Flag: Direct dict manipulation instead of entities, missing schema validation
Entity Pattern:
@dataclass
class Job(Resource):
"""Job entity."""
name: str
experiment_name: Optional[str] = None
def _to_rest_object(self) -> RestJob:
"""Convert to REST representation."""
...
@classmethod
def _from_rest_object(cls, obj: RestJob) -> "Job":
"""Create from REST representation."""
...
8. Testing Patterns
- Check: Proper unit tests, recorded tests for operations
- Look for: Use of
pytest, proper test isolation, fixture usage
- Flag: Missing tests for new features, tests with external dependencies, hardcoded values
Test Structure:
class TestJobOperations:
"""Test job operations."""
def test_create_job(self, client: MLClient, mock_workspace: Mock) -> None:
"""Test job creation."""
job = Job(name="test-job")
result = client.jobs.create_or_update(job)
assert result.name == "test-job"
@pytest.mark.recorded
def test_get_job_recorded(self, client: MLClient) -> None:
"""Test getting job with recording."""
...
9. Documentation & Docstrings
- Check: Complete docstrings following Google/NumPy style
- Look for: Parameter descriptions, return types, examples, raises
- Flag: Missing docstrings on public APIs, incomplete parameter docs
Docstring Pattern:
def create_or_update(
self,
job: Job,
**kwargs: Any
) -> Job:
"""Create or update a job.
:param job: The job entity to create or update.
:type job: ~azure.ai.ml.entities.Job
:keyword bool skip_validation: Skip validation of the job.
:return: The created or updated job.
:rtype: ~azure.ai.ml.entities.Job
:raises ~azure.core.exceptions.HttpResponseError: If the request fails.
.. admonition:: Example:
.. code-block:: python
from azure.ai.ml.entities import Job
job = Job(name="my-job")
result = ml_client.jobs.create_or_update(job)
"""
10. Backwards Compatibility
- Check: No breaking changes without major version bump
- Look for: Deprecated parameters, migration paths, version notes
- Flag: Removing public methods, changing signatures, removing parameters
Deprecation Pattern:
import warnings
def old_method(self, param: str) -> None:
"""Deprecated method.
.. deprecated:: 1.2.0
Use :meth:`new_method` instead.
"""
warnings.warn(
"old_method is deprecated, use new_method instead",
DeprecationWarning,
stacklevel=2
)
self.new_method(param)
11. Security & Credentials
- Check: Proper credential handling, no secrets in logs
- Look for: Use of
TokenCredential, proper token refresh, sanitized logging
- Flag: Credentials in error messages, API keys in code, secrets in tests
Pattern:
from azure.core.credentials import TokenCredential
class MLClient:
def __init__(
self,
credential: TokenCredential,
subscription_id: str,
**kwargs: Any
):
self._credential = credential # Store, don't log
# Never log credential or tokens
12. Performance & Efficiency
- Check: Efficient API calls, proper pagination, lazy loading
- Look for: Batching operations, caching where appropriate, avoiding N+1 queries
- Flag: Loading all items in memory, multiple API calls in loops, no pagination
Pagination Pattern:
def list(self, **kwargs: Any) -> Iterable[Job]:
"""List jobs with pagination.
:return: An iterable of jobs.
:rtype: ~azure.core.paging.ItemPaged[~azure.ai.ml.entities.Job]
"""
return self._operation.list(...) # Returns ItemPaged
Analysis Instructions
- Get uncommitted changes: Use git to identify modified files in
sdk/ml/azure-ai-ml/
- Read changed sections: Focus on modified lines and surrounding context
- Check each focus area: Go through all 12 areas systematically
- Priority levels: Critical (breaking/security) > High (bugs/types) > Medium (style/docs)
- Provide specific examples: Show actual code with file paths and line numbers
- Cross-reference: Check consistency across sync/async, operations/entities
Output Format
Organize findings by priority and category:
✅ Positive Observations
Good patterns worth highlighting
🔴 Critical Issues
- Breaking changes without migration path
- Missing credential validation
- Type safety violations causing runtime errors
- Security vulnerabilities
⚠️ High Priority Issues
- Missing type annotations on public APIs
- Pylint/MyPy errors
- Missing tests for new functionality
- Improper async patterns
📋 Medium Priority Issues
- Missing or incomplete docstrings
- Code style inconsistencies
- Performance optimizations
- Better error messages
💡 Suggestions
- Refactoring opportunities
- Additional test coverage
- Documentation improvements
For each issue:
- Location: File path and line numbers
- Current code: Show the problematic code
- Issue: Explain what's wrong and why
- Recommended fix: Show corrected code
- References: Link to relevant guidelines
Summary
- Total files changed: X
- Critical issues: X
- High priority: X
- Medium priority: X
- Overall assessment: Ready/Needs work/Blocked
Focus on issues that impact SDK quality, user experience, backwards compatibility, and Azure SDK guideline compliance.
1---2name: do-code-review3description: Reviews code changes in azure-ai-ml package for quality, Azure SDK compliance, and best practices. Use when reviewing code, checking pull requests, or when user asks to review changes or check code quality in azure-ai-ml.4metadata:5 source: https://skillmd.com/skills/azure-azure-sdk-for-python/do-code-review6---78# Azure AI ML Code Review910Reviews uncommitted changes (staged and unstaged files) in the azure-ai-ml package, focusing on Azure SDK Python design guidelines, type safety, testing patterns, and API consistency.1112## Default Review Scope1314Unless otherwise specified, review all uncommitted changes in the current branch (staged and unstaged files) within `sdk/ml/azure-ai-ml/`. This includes new files, modified files, and any pending changes that haven't been committed yet.1516## Review Focus Areas1718### 1. Azure SDK Design Guidelines Compliance1920- **Check**: Adherence to [Azure SDK Python Design Guidelines](https://azure.github.io/azure-sdk/python_design.html)21- **Look for**: Proper client naming, method patterns, parameter order22- **Flag**: Non-compliant naming (use `create_or_update` not `create_or_replace`)23- **Example Issue**: `def get_job(name, subscription_id)` should be `def get_job(name, **kwargs)`2425**Key Patterns:**26- Client methods: `begin_*` for LROs, `list_*` for paginators27- Naming: snake_case for methods, PascalCase for classes28- Parameters: Required positional, optional keyword-only29- Return types: Explicit type hints for all public APIs3031### 2. Type Annotations & MyPy Compliance3233- **Check**: Complete type annotations on all public APIs34- **Look for**: Proper use of `Optional`, `Union`, `TYPE_CHECKING`35- **Flag**: Missing return types, `Any` without justification, bare `dict`/`list`36- **Example Issue**: `def process_data(data)` should be `def process_data(data: Dict[str, Any]) -> ProcessedData`3738**Common Fixes:**39```python40# Bad41def get_config(name):42 return config4344# Good45def get_config(name: str) -> Optional[Dict[str, Any]]:46 return config47```4849### 3. Pylint Compliance5051- **Check**: Code passes pylint with azure-sdk-for-python rules52- **Look for**: Proper docstrings, no unused imports, correct argument names53- **Flag**: Violations of naming conventions, too many arguments (>5), long lines (>120)54- **Reference**: [Azure Pylint Guidelines](https://github.com/Azure/azure-sdk-tools/blob/main/tools/pylint-extensions/azure-pylint-guidelines-checker/README.md)5556**Watch for:**57- `client-method-should-not-use-static-method`58- `missing-client-constructor-parameter-credential`59- `client-method-has-more-than-5-positional-arguments`6061### 4. Async/Await Patterns6263- **Check**: Proper async implementation in `_async` modules64- **Look for**: Using `async with` for clients, awaiting coroutines correctly65- **Flag**: Blocking calls in async code, missing `await`, sync code in async modules66- **Example Issue**: `self._client.get()` in async should be `await self._client.get()`6768**Pattern:**69```python70# In azure/ai/ml/aio/operations/71async def create_or_update(72 self,73 entity: Job,74 **kwargs: Any75) -> Job:76 async with self._lock:77 result = await self._service_client.create_or_update(...)78 return result79```8081### 5. Error Handling & Validation8283- **Check**: Proper exception handling with Azure SDK exceptions84- **Look for**: Use of `HttpResponseError`, `ResourceNotFoundError`, proper validation85- **Flag**: Bare `except:`, catching `Exception` without re-raising, missing validation86- **Example Issue**: Missing parameter validation before API calls8788**Pattern:**89```python90from azure.core.exceptions import ResourceNotFoundError, HttpResponseError9192try:93 result = self._operation.get(name)94except ResourceNotFoundError:95 raise ResourceNotFoundError(f"Job '{name}' not found")96except HttpResponseError as e:97 raise HttpResponseError(f"Failed to retrieve job: {e.message}")98```99100### 6. API Design & Operations101102- **Check**: Consistent CRUD patterns across operations classes103- **Look for**: Proper separation of sync/async, operations returning correct types104- **Flag**: Business logic in client, missing operations class, inconsistent method names105106**Structure:**107```108azure/ai/ml/109├── operations/ # Sync operations110│ ├── job_operations.py111│ └── model_operations.py112└── aio/operations/ # Async operations (mirror structure)113 ├── job_operations.py114 └── model_operations.py115```116117### 7. Entity & Schema Patterns118119- **Check**: Proper use of marshmallow schemas, correct entity inheritance120- **Look for**: Schema validation, proper serialization/deserialization121- **Flag**: Direct dict manipulation instead of entities, missing schema validation122123**Entity Pattern:**124```python125@dataclass126class Job(Resource):127 """Job entity."""128 129 name: str130 experiment_name: Optional[str] = None131 132 def _to_rest_object(self) -> RestJob:133 """Convert to REST representation."""134 ...135 136 @classmethod137 def _from_rest_object(cls, obj: RestJob) -> "Job":138 """Create from REST representation."""139 ...140```141142### 8. Testing Patterns143144- **Check**: Proper unit tests, recorded tests for operations145- **Look for**: Use of `pytest`, proper test isolation, fixture usage146- **Flag**: Missing tests for new features, tests with external dependencies, hardcoded values147148**Test Structure:**149```python150class TestJobOperations:151 """Test job operations."""152 153 def test_create_job(self, client: MLClient, mock_workspace: Mock) -> None:154 """Test job creation."""155 job = Job(name="test-job")156 result = client.jobs.create_or_update(job)157 assert result.name == "test-job"158 159 @pytest.mark.recorded160 def test_get_job_recorded(self, client: MLClient) -> None:161 """Test getting job with recording."""162 ...163```164165### 9. Documentation & Docstrings166167- **Check**: Complete docstrings following Google/NumPy style168- **Look for**: Parameter descriptions, return types, examples, raises169- **Flag**: Missing docstrings on public APIs, incomplete parameter docs170171**Docstring Pattern:**172```python173def create_or_update(174 self,175 job: Job,176 **kwargs: Any177) -> Job:178 """Create or update a job.179 180 :param job: The job entity to create or update.181 :type job: ~azure.ai.ml.entities.Job182 :keyword bool skip_validation: Skip validation of the job.183 :return: The created or updated job.184 :rtype: ~azure.ai.ml.entities.Job185 :raises ~azure.core.exceptions.HttpResponseError: If the request fails.186 187 .. admonition:: Example:188 189 .. code-block:: python190 191 from azure.ai.ml.entities import Job192 job = Job(name="my-job")193 result = ml_client.jobs.create_or_update(job)194 """195```196197### 10. Backwards Compatibility198199- **Check**: No breaking changes without major version bump200- **Look for**: Deprecated parameters, migration paths, version notes201- **Flag**: Removing public methods, changing signatures, removing parameters202203**Deprecation Pattern:**204```python205import warnings206207def old_method(self, param: str) -> None:208 """Deprecated method.209 210 .. deprecated:: 1.2.0211 Use :meth:`new_method` instead.212 """213 warnings.warn(214 "old_method is deprecated, use new_method instead",215 DeprecationWarning,216 stacklevel=2217 )218 self.new_method(param)219```220221### 11. Security & Credentials222223- **Check**: Proper credential handling, no secrets in logs224- **Look for**: Use of `TokenCredential`, proper token refresh, sanitized logging225- **Flag**: Credentials in error messages, API keys in code, secrets in tests226227**Pattern:**228```python229from azure.core.credentials import TokenCredential230231class MLClient:232 def __init__(233 self,234 credential: TokenCredential,235 subscription_id: str,236 **kwargs: Any237 ):238 self._credential = credential # Store, don't log239 # Never log credential or tokens240```241242### 12. Performance & Efficiency243244- **Check**: Efficient API calls, proper pagination, lazy loading245- **Look for**: Batching operations, caching where appropriate, avoiding N+1 queries246- **Flag**: Loading all items in memory, multiple API calls in loops, no pagination247248**Pagination Pattern:**249```python250def list(self, **kwargs: Any) -> Iterable[Job]:251 """List jobs with pagination.252 253 :return: An iterable of jobs.254 :rtype: ~azure.core.paging.ItemPaged[~azure.ai.ml.entities.Job]255 """256 return self._operation.list(...) # Returns ItemPaged257```258259## Analysis Instructions2602611. **Get uncommitted changes**: Use git to identify modified files in `sdk/ml/azure-ai-ml/`2622. **Read changed sections**: Focus on modified lines and surrounding context2633. **Check each focus area**: Go through all 12 areas systematically2644. **Priority levels**: Critical (breaking/security) > High (bugs/types) > Medium (style/docs)2655. **Provide specific examples**: Show actual code with file paths and line numbers2666. **Cross-reference**: Check consistency across sync/async, operations/entities267268## Output Format269270Organize findings by priority and category:271272### ✅ Positive Observations273Good patterns worth highlighting274275### 🔴 Critical Issues276- Breaking changes without migration path277- Missing credential validation278- Type safety violations causing runtime errors279- Security vulnerabilities280281### ⚠️ High Priority Issues282- Missing type annotations on public APIs283- Pylint/MyPy errors284- Missing tests for new functionality285- Improper async patterns286287### 📋 Medium Priority Issues288- Missing or incomplete docstrings289- Code style inconsistencies290- Performance optimizations291- Better error messages292293### 💡 Suggestions294- Refactoring opportunities295- Additional test coverage296- Documentation improvements297298For each issue:2991. **Location**: File path and line numbers3002. **Current code**: Show the problematic code3013. **Issue**: Explain what's wrong and why3024. **Recommended fix**: Show corrected code3035. **References**: Link to relevant guidelines304305### Summary306307- Total files changed: X308- Critical issues: X309- High priority: X 310- Medium priority: X311- Overall assessment: Ready/Needs work/Blocked312313Focus on issues that impact SDK quality, user experience, backwards compatibility, and Azure SDK guideline compliance.