Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SNOW-1417060: Pylance throws errors when using copy_options with copy_into_location method #1575

Closed

Conversation

Yoshi-Egawa
Copy link
Contributor

@Yoshi-Egawa Yoshi-Egawa commented May 14, 2024

  1. Which Jira issue is this PR addressing? Make sure that there is an accompanying issue to your PR.

    Fixes SNOW-1417060

  2. Fill out the following pre-review checklist:

    • I am adding a new automated test(s) to verify correctness of my new code
    • I am adding new logging messages
    • I am adding a new telemetry message
    • I am adding new credentials
    • I am adding a new dependency
  3. Please describe how your code solves the related issue.

    Instead of using Dict[str, Any] for the copy_options argument, I have switched to a TypedDict to enforce stricter type checking and provide better clarity on the expected options.

Comment on lines 839 to 844
class CopyOptions(TypedDict, total=False):
overwrite: bool
single: bool
max_file_size: float
include_query_id: bool
detailed_output: bool
Copy link
Contributor Author

@Yoshi-Egawa Yoshi-Egawa May 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This allows the IDE to tell you when it receives unexpected keyword arguments.

image

  • using python 3.8 in vscode devcontainer.

Comment on lines 267 to 281
@overload
def copy_into_location(
self,
location: str,
*,
partition_by: Optional[ColumnOrSqlExpr] = None,
file_format_name: Optional[str] = None,
file_format_type: Optional[str] = None,
format_type_options: Optional[Dict[str, str]] = None,
header: bool = False,
statement_params: Optional[Dict[str, str]] = None,
block: bool = True,
**copy_options: Unpack[CopyOptions],
) -> Union[List[Row], AsyncJob]:
... # pragma: no cover
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type hint of a function definition without overload did not work with csv method or json method, and a pylancen error occurred, so I also defined it with overload.

@@ -834,3 +835,10 @@ def type_string_to_type_object(type_str: str) -> DataType:
ColumnOrSqlExpr = Union["snowflake.snowpark.column.Column", str]
LiteralType = Union[VALID_PYTHON_TYPES_FOR_LITERAL_VALUE]
ColumnOrLiteral = Union["snowflake.snowpark.column.Column", LiteralType]

class CopyOptions(TypedDict, total=False):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TypedDict and total arguments are supported starting with Python 3.8.

https://docs.python.org/3.8/library/typing.html?highlight=typeddict#typing.TypedDict

@@ -4,6 +4,7 @@

import sys
from typing import Any, Dict, List, Literal, Optional, Union, overload
from typing_extensions import Unpack
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Yoshi-Egawa
Copy link
Contributor Author

I have read the CLA Document and I hereby sign the CLA

@Yoshi-Egawa
Copy link
Contributor Author

Are you looking at the PR?

@Yoshi-Egawa Yoshi-Egawa reopened this Jun 25, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Jun 25, 2024
@Yoshi-Egawa Yoshi-Egawa force-pushed the feature/copy_options-type branch from 38fc8e6 to 625519f Compare June 25, 2024 13:59
@Yoshi-Egawa Yoshi-Egawa force-pushed the feature/copy_options-type branch from 625519f to 85cb3c7 Compare June 25, 2024 14:01
@Yoshi-Egawa Yoshi-Egawa deleted the feature/copy_options-type branch June 25, 2024 14:20
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant