Skip to content

Declare ParseType/BuildTypes as covariant/contravariant, respectively - #1

Closed
ad1217 wants to merge 2 commits into
timrid:mainfrom
ad1217:parsetype-buildtypes-variance
Closed

ad1217 wants to merge 2 commits into
timrid:mainfrom
ad1217:parsetype-buildtypes-variance

Conversation

@ad1217

@ad1217 ad1217 commented Mar 10, 2021

Copy link
Copy Markdown

This allows for passing Constructs with subclass/superclass ParseType/BuildTypes to Subconstructs, as would be expected.

The covariance test is admittedly not a great example; my actual reason is for this is to have correctly typed arguments in a TStruct, but that wouldn't belong in the core tests:

@dataclass
class Whatever(cst.TContainerBase):
    misc: list[int] = cst.TStructField(cs.Byte[4])

@arekbulski

Copy link
Copy Markdown

Did you notice that your changes broke it? Tests failed.

@ad1217
ad1217 force-pushed the parsetype-buildtypes-variance branch from 81e7b4d to 78e530f Compare March 10, 2021 22:01
ad1217 added 2 commits March 10, 2021 17:04
This allows for passing Constructs with subclass/superclass
ParseType/BuildTypes to Subconstructs, as would be expected
@ad1217
ad1217 force-pushed the parsetype-buildtypes-variance branch from 78e530f to 604c96c Compare March 10, 2021 22:04
@ad1217

ad1217 commented Mar 10, 2021

Copy link
Copy Markdown
Author

Ah, yeah. I forgot I was only running the test_core tests... 🤦 Should be fixed now, but I'm not entirely sure my change to the typing of Default is correct.

@arekbulski

Copy link
Copy Markdown

Tim owns this repository so I cannot give you an authoritative answer.

@timrid

timrid commented Mar 11, 2021

Copy link
Copy Markdown
Owner

I also had the same problem with the List as StructField. My solution was to include the type alias List = t.Union[ListContainer[ListType], t.List[ListType]] in the package which could be used. But to use covariant seems much better. And also contravariant makes sense.

I looked at the problem with Default. Without your change pyright works but mypy reports:

tests\test_typed.py:163: error: Argument 1 to "Default" has incompatible type "Bytes[bytes, Union[ByteString, int]]"; expected "Construct[bytes, object]"

and with your change mypy works but pyright reports at the same line:

Argument of type "Default[bytes, bytes]" cannot be assigned to parameter "subcon" of type "Construct[ParsedType@TStructField, BuildTypes@TStructField]" in function "TStructField"
  TypeVar "BuildTypes@Subconstruct" is contravariant
    Type "SubconBuildTypes@Default | None" cannot be assigned to type "bytes | None"
      Type "bytes | None" cannot be assigned to type "bytes"PylancereportGeneralTypeIssues

I'll have to take a closer look this weekend.

(That the package arrow supports PEP 484 compliant type annotations since two weeks is also good to know.)

timrid added a commit that referenced this pull request Mar 13, 2021
@timrid

timrid commented Mar 13, 2021

Copy link
Copy Markdown
Owner

The implementation of Default is correct as it was. The problem of the mypy error was located in TStructField.

def TStructField(
    subcon: Construct[ParsedType, BuildTypes],
    ...

has to be changed to

def TStructField(
    subcon: Construct[ParsedType, t.Any],
    ...

I will apply your modifications together with other modifications I am doing right now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants