KEMBAR78
Add type inference for functions without OutputType attribute and anonymous functions by MartinGC94 · Pull Request #21127 · PowerShell/PowerShell · GitHub
Skip to content

Conversation

@MartinGC94
Copy link
Contributor

PR Summary

This adds type inference for functions where the OutputType attribute hasn't been filled out and for anonymous functions.
This allows tab completion to work like you would expect in situations like:

function MyFunction
{
    [pscustomobject]@{Property1 = "Hello"; Property2 = "Hi"}
}
MyFunction | Select <Tab>
# And also:
& {[pscustomobject]@{Property1 = "Hello"; Property2 = "Hi"}} | Select <Tab>

PR Context

PR Checklist

@MartinGC94
Copy link
Contributor Author

MartinGC94 commented Jan 22, 2024

I've got 2 test failures that I'm not sure how to handle.
1: Infers type from variable with AllowSafeEval it fails because it relied on the idea that type inference wouldn't be able to infer the type of a function with no OutputType defined. With that limitation removed, I'm not sure how to test that AllowSafeEval works.
2: Should infer output from function without OutputType attribute (one of the tests I added) it seems to fail because the pseudobinding fails when running the InferTypeOf method. It works fine when tab completing though. The easiest fix for me would be to just add a tab completion test instead, but if there is a way to make it work the same as the rest of the type inference tests then that would be ideal.

-Edit: Fixed my own test by moving the function definition outside the member invocation because the ExportVisitor doesn't visit child nodes of member invocations.

@pull-request-quantifier-deprecated

This PR has 16 quantified lines of changes. In general, a change size of upto 200 lines is ideal for the best PR experience!


Quantification details

Label      : Extra Small
Size       : +16 -0
Percentile : 6.4%

Total files changed: 2

Change summary by file extension:
.cs : +10 -0
.ps1 : +6 -0

Change counts above are quantified counts, based on the PullRequestQuantifier customizations.

Why proper sizing of changes matters

Optimal pull request sizes drive a better predictable PR flow as they strike a
balance between between PR complexity and PR review overhead. PRs within the
optimal size (typical small, or medium sized PRs) mean:

  • Fast and predictable releases to production:
    • Optimal size changes are more likely to be reviewed faster with fewer
      iterations.
    • Similarity in low PR complexity drives similar review times.
  • Review quality is likely higher as complexity is lower:
    • Bugs are more likely to be detected.
    • Code inconsistencies are more likely to be detected.
  • Knowledge sharing is improved within the participants:
    • Small portions can be assimilated better.
  • Better engineering practices are exercised:
    • Solving big problems by dividing them in well contained, smaller problems.
    • Exercising separation of concerns within the code changes.

What can I do to optimize my changes

  • Use the PullRequestQuantifier to quantify your PR accurately
    • Create a context profile for your repo using the context generator
    • Exclude files that are not necessary to be reviewed or do not increase the review complexity. Example: Autogenerated code, docs, project IDE setting files, binaries, etc. Check out the Excluded section from your prquantifier.yaml context profile.
    • Understand your typical change complexity, drive towards the desired complexity by adjusting the label mapping in your prquantifier.yaml context profile.
    • Only use the labels that matter to you, see context specification to customize your prquantifier.yaml context profile.
  • Change your engineering behaviors
    • For PRs that fall outside of the desired spectrum, review the details and check if:
      • Your PR could be split in smaller, self-contained PRs instead
      • Your PR only solves one particular issue. (For example, don't refactor and code new features in the same PR).

How to interpret the change counts in git diff output

  • One line was added: +1 -0
  • One line was deleted: +0 -1
  • One line was modified: +1 -1 (git diff doesn't know about modified, it will
    interpret that line like one addition plus one deletion)
  • Change percentiles: Change characteristics (addition, deletion, modification)
    of this PR in relation to all other PRs within the repository.


Was this comment helpful? 👍  :ok_hand:  :thumbsdown: (Email)
Customize PullRequestQuantifier for this repository.

@microsoft-github-policy-service microsoft-github-policy-service bot added the Review - Needed The PR is being reviewed label Jan 30, 2024
@pull-request-quantifier-deprecated

This PR has 16 quantified lines of changes. In general, a change size of upto 200 lines is ideal for the best PR experience!


Quantification details

Label      : Extra Small
Size       : +16 -0
Percentile : 6.4%

Total files changed: 2

Change summary by file extension:
.cs : +10 -0
.ps1 : +6 -0

Change counts above are quantified counts, based on the PullRequestQuantifier customizations.

Why proper sizing of changes matters

Optimal pull request sizes drive a better predictable PR flow as they strike a
balance between between PR complexity and PR review overhead. PRs within the
optimal size (typical small, or medium sized PRs) mean:

  • Fast and predictable releases to production:
    • Optimal size changes are more likely to be reviewed faster with fewer
      iterations.
    • Similarity in low PR complexity drives similar review times.
  • Review quality is likely higher as complexity is lower:
    • Bugs are more likely to be detected.
    • Code inconsistencies are more likely to be detected.
  • Knowledge sharing is improved within the participants:
    • Small portions can be assimilated better.
  • Better engineering practices are exercised:
    • Solving big problems by dividing them in well contained, smaller problems.
    • Exercising separation of concerns within the code changes.

What can I do to optimize my changes

  • Use the PullRequestQuantifier to quantify your PR accurately
    • Create a context profile for your repo using the context generator
    • Exclude files that are not necessary to be reviewed or do not increase the review complexity. Example: Autogenerated code, docs, project IDE setting files, binaries, etc. Check out the Excluded section from your prquantifier.yaml context profile.
    • Understand your typical change complexity, drive towards the desired complexity by adjusting the label mapping in your prquantifier.yaml context profile.
    • Only use the labels that matter to you, see context specification to customize your prquantifier.yaml context profile.
  • Change your engineering behaviors
    • For PRs that fall outside of the desired spectrum, review the details and check if:
      • Your PR could be split in smaller, self-contained PRs instead
      • Your PR only solves one particular issue. (For example, don't refactor and code new features in the same PR).

How to interpret the change counts in git diff output

  • One line was added: +1 -0
  • One line was deleted: +0 -1
  • One line was modified: +1 -1 (git diff doesn't know about modified, it will
    interpret that line like one addition plus one deletion)
  • Change percentiles: Change characteristics (addition, deletion, modification)
    of this PR in relation to all other PRs within the repository.


Was this comment helpful? 👍  :ok_hand:  :thumbsdown: (Email)
Customize PullRequestQuantifier for this repository.

@pull-request-quantifier-deprecated

This PR has 17 quantified lines of changes. In general, a change size of upto 200 lines is ideal for the best PR experience!


Quantification details

Label      : Extra Small
Size       : +17 -0
Percentile : 6.8%

Total files changed: 2

Change summary by file extension:
.cs : +10 -0
.ps1 : +7 -0

Change counts above are quantified counts, based on the PullRequestQuantifier customizations.

Why proper sizing of changes matters

Optimal pull request sizes drive a better predictable PR flow as they strike a
balance between between PR complexity and PR review overhead. PRs within the
optimal size (typical small, or medium sized PRs) mean:

  • Fast and predictable releases to production:
    • Optimal size changes are more likely to be reviewed faster with fewer
      iterations.
    • Similarity in low PR complexity drives similar review times.
  • Review quality is likely higher as complexity is lower:
    • Bugs are more likely to be detected.
    • Code inconsistencies are more likely to be detected.
  • Knowledge sharing is improved within the participants:
    • Small portions can be assimilated better.
  • Better engineering practices are exercised:
    • Solving big problems by dividing them in well contained, smaller problems.
    • Exercising separation of concerns within the code changes.

What can I do to optimize my changes

  • Use the PullRequestQuantifier to quantify your PR accurately
    • Create a context profile for your repo using the context generator
    • Exclude files that are not necessary to be reviewed or do not increase the review complexity. Example: Autogenerated code, docs, project IDE setting files, binaries, etc. Check out the Excluded section from your prquantifier.yaml context profile.
    • Understand your typical change complexity, drive towards the desired complexity by adjusting the label mapping in your prquantifier.yaml context profile.
    • Only use the labels that matter to you, see context specification to customize your prquantifier.yaml context profile.
  • Change your engineering behaviors
    • For PRs that fall outside of the desired spectrum, review the details and check if:
      • Your PR could be split in smaller, self-contained PRs instead
      • Your PR only solves one particular issue. (For example, don't refactor and code new features in the same PR).

How to interpret the change counts in git diff output

  • One line was added: +1 -0
  • One line was deleted: +0 -1
  • One line was modified: +1 -1 (git diff doesn't know about modified, it will
    interpret that line like one addition plus one deletion)
  • Change percentiles: Change characteristics (addition, deletion, modification)
    of this PR in relation to all other PRs within the repository.


Was this comment helpful? 👍  :ok_hand:  :thumbsdown: (Email)
Customize PullRequestQuantifier for this repository.

@iSazonov
Copy link
Collaborator

@MartinGC94 Please resolve merge conflicts and rebase.

@microsoft-github-policy-service microsoft-github-policy-service bot removed the Review - Needed The PR is being reviewed label Feb 26, 2025
@iSazonov

This comment was marked as outdated.

@azure-pipelines

This comment was marked as outdated.

@iSazonov iSazonov self-assigned this Feb 26, 2025
@iSazonov iSazonov added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Feb 26, 2025
@iSazonov
Copy link
Collaborator

iSazonov commented Feb 27, 2025

As for failed test. I see OutputType attribute in Get-Process hasn't parameter sets. So I guess with the change we take all types from the attribute instead of doing type inference based on real objects.

@MartinGC94
Copy link
Contributor Author

I figured out a way: I can just hide it behind Invoke-Expression and as long as we don't try to make the type inference for Invoke-Expression work, the test will run.

@iSazonov

This comment was marked as outdated.

@azure-pipelines

This comment was marked as outdated.

@iSazonov

This comment was marked as outdated.

@azure-pipelines

This comment was marked as outdated.

# Invoke-Expression is used to "hide" Get-Process from the type inference.
# If the typeinference code is updated to handle Invoke-Expression, this test will need to find some other way to set $p
# so that the type inference can't figure it out without evaluating the variable value
$p = Invoke-Expression -Command 'Get-Process'
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we use the pattern

Describe "AstTypeInference tests" -Tags CI {
It "Infers type from integer with passed in powershell instance" {
$powerShell = [PowerShell]::Create([RunspaceMode]::CurrentRunspace)
$res = [AstTypeInference]::InferTypeOf( { 1 }.Ast, $powerShell)
$res.Count | Should -Be 1
$res.Name | Should -Be 'System.Int32'
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No because the type inference based on the variable runtime value is a last resort: https://github.com/PowerShell/PowerShell/blob/master/src/System.Management.Automation/engine/parser/TypeInferenceVisitor.cs#L2485
The regular type inference will still see the variable being assigned by the command Get-Process and hence, use the output type defined there.
I think the only way to make the test fully robust is to make a test specific Get-Process2 cmdlet without output types defined so that the type inference has no way of finding out the value without the SafeEval.

@iSazonov
Copy link
Collaborator

@MartinGC94 Please resolve merge conflicts.

@iSazonov
Copy link
Collaborator

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 2 pipeline(s).

@iSazonov iSazonov enabled auto-merge (squash) February 28, 2025 15:22
@iSazonov iSazonov merged commit 33ed509 into PowerShell:master Feb 28, 2025
39 of 41 checks passed
@microsoft-github-policy-service
Copy link
Contributor

microsoft-github-policy-service bot commented Feb 28, 2025

📣 Hey @MartinGC94, how did we do? We would love to hear your feedback with the link below! 🗣️

🔗 https://aka.ms/PSRepoFeedback

@MartinGC94 MartinGC94 deleted the ImproveCmdTypeInference branch February 28, 2025 18:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log Extra Small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants